如何使用自身原型访问 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP