【发布时间】:2019-01-12 02:54:22
【问题描述】:
我很难理解为什么要在构造函数类或其原型对象上定义属性。
这里是我对原型的理解 - 在原型中声明属性(而不是链接的父对象)可以节省性能,因为每个子对象都不会拥有自己的父属性副本。
问题:但我认为您不能从非原始类型复制值,即函数对象只能传递引用......并且只能从原始类型复制?
**这是否意味着如果我继承父类的方法,如下所示,我正在复制对方法的引用或实际复制? **
function Parent() {
this.name = "jeff";
}
var child = new Parent();
console.log(child.name); /// is copied from parent or the reference is copied??
在下面的例子中,我引用了原型……对吗?
Parent.prototype.age = 9;
child.age // I looks at the parent class, then reference to prototype.age.
****问题2:**如果我可以更改特定对象的prototype.age,那么我实际上是复制了该值,对吗?那么重点是什么,即**
child.age = 10; // changed the value for THIS object
【问题讨论】:
-
抱歉,错过了新关键字。
标签: javascript inheritance prototype prototypal-inheritance