如何使用自己的原型访问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 妈妈生日祝福语简短励志