如何使用自己的原型访问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 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短