如何使用自己的原型访问JavaScript对象?
我们可以通过使用名为“Object.create()”的javascript方法创建自己的原型来访问现有对象。使用此方法,我们可以将属性从现有属性继承到新创建的原型。让我们简要地讨论一下。
语法
Object.create(existing obj);
此方法采用现有对象并创建自己的原型,以便将属性从现有对象继承到新创建的原型。
示例
在下面的示例中,最初将创建一个名为“person”的对象,并使用其自己的原型创建“Object.create”并将其分配给变量“newper”。稍后,使用原型更改了现有对象的对象,并显示了新属性,如输出所示。
<html>
<body>
<script>
var person = {
name: "Karthee",
profession : "Actor",
industry: "Tamil"
};
document.write(person.name);
document.write("</br>");
document.write(person.profession);
document.write("</br>");
document.write(person.industry);
document.write("</br>");
document.write("Using a prototype the properties of the existing object have been
changed to the following");
document.write("</br>");
var newper = Object.create(person); /// creating prototype
newper.name = "sachin";
newper.profession = "crickter";
newper.industry = "sports";
document.write(newper.name);
document.write("</br>");
document.write(newper.profession);
document.write("</br>");
document.write(newper.industry);
</script>
</body>
</html>输出结果
Karthee Actor Tamil Using a prototype the properties of the existing object have been changed to the following sachin crickter sports
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短