如何使用自己的原型访问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 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短