如何使用自己的原型访问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 祝福语驱散霉运的话简短