原型继承和原型对象,为什么没有用到这个程度呢?
prototype inheritance and the prototype object, why isn't used to this extent?
我快速搜索了一下,但似乎找不到这个问题的答案,只是指在继承时复制函数原型。
为什么不给构造函数原型obj添加属性,而是使用this关键字。我确定有理由不这样做,但我试图更好地理解 javascript 的细微差别。例如在正常的原型继承中你会 "this".
function Dog(name,age,breed){
this.name=name;
this.age=age;
this.breed=breed;
}
Dog.prototype.bark=function(){console.log("bark bark bark");}
let spike=new Dog("Spike",6,"lab");
let rover=new Dog("Rover",8,"poodle");
//^with the constructor function, no instance has the bark function but
//but refers to the same function on the constructor prototype obj, so the
//same function isn't being duplicated. However new keyword changes the
//context of this to refer to the obj so these properties are duplicated
//on every instance.
//I'm curious as to the reason why new doesn't change this to refer to the
//prototype obj for example and then have the instance refers its
//constructor's prototype like with the bark function?
//for example why isn't this a common pattern and what are the reasons I
//should use it.
function Dog(name,age,breed){
Dog.prototype.name=name;
Dog.prototype.age=age;
Dog.prototype.breed=breed;
}
let spike=new Dog("Spike",6,"lab");
let rover=new Dog("rover",8,"poodle");
//I feel like the above code would be more DRY, I'm sure there is a reason
// this isn't common and I'm curious as to why
当原型上有 properties
时,每次实例化 Class 时都会用新值覆盖属性,即在您的示例中,来自以下两个语句:
let spike=new Dog("Spike",6,"lab");
let rover=new Dog("rover",8,"poodle");
这里,按照你的预期,spike.name
应该是Spike
,rover.name
应该是rover
,但是如果你执行这段代码,检查一下,两者都是是 rover
。
当您创建新实例 rover
时,spike
的属性被 rover
的属性覆盖。
每次创建一个新实例时,属性都会被覆盖,原因是
附加到原型的 methods
或 properties
仅创建一次,并在每次创建新实例时继承给它们的子 类。
我们创建构造函数并从中创建新实例的原因是因为我们对每个实例都有不同的属性,例如 Spike
和 rover
。在方法的情况下,方法对于构造函数是通用的,它可以重复用于不需要每次创建新实例时都创建的所有实例,因此,我们将它们附加到 prototype
而不是用 this
构造函数中的关键字。
我快速搜索了一下,但似乎找不到这个问题的答案,只是指在继承时复制函数原型。 为什么不给构造函数原型obj添加属性,而是使用this关键字。我确定有理由不这样做,但我试图更好地理解 javascript 的细微差别。例如在正常的原型继承中你会 "this".
function Dog(name,age,breed){
this.name=name;
this.age=age;
this.breed=breed;
}
Dog.prototype.bark=function(){console.log("bark bark bark");}
let spike=new Dog("Spike",6,"lab");
let rover=new Dog("Rover",8,"poodle");
//^with the constructor function, no instance has the bark function but
//but refers to the same function on the constructor prototype obj, so the
//same function isn't being duplicated. However new keyword changes the
//context of this to refer to the obj so these properties are duplicated
//on every instance.
//I'm curious as to the reason why new doesn't change this to refer to the
//prototype obj for example and then have the instance refers its
//constructor's prototype like with the bark function?
//for example why isn't this a common pattern and what are the reasons I
//should use it.
function Dog(name,age,breed){
Dog.prototype.name=name;
Dog.prototype.age=age;
Dog.prototype.breed=breed;
}
let spike=new Dog("Spike",6,"lab");
let rover=new Dog("rover",8,"poodle");
//I feel like the above code would be more DRY, I'm sure there is a reason
// this isn't common and I'm curious as to why
当原型上有 properties
时,每次实例化 Class 时都会用新值覆盖属性,即在您的示例中,来自以下两个语句:
let spike=new Dog("Spike",6,"lab");
let rover=new Dog("rover",8,"poodle");
这里,按照你的预期,spike.name
应该是Spike
,rover.name
应该是rover
,但是如果你执行这段代码,检查一下,两者都是是 rover
。
当您创建新实例 rover
时,spike
的属性被 rover
的属性覆盖。
每次创建一个新实例时,属性都会被覆盖,原因是
附加到原型的 methods
或 properties
仅创建一次,并在每次创建新实例时继承给它们的子 类。
我们创建构造函数并从中创建新实例的原因是因为我们对每个实例都有不同的属性,例如 Spike
和 rover
。在方法的情况下,方法对于构造函数是通用的,它可以重复用于不需要每次创建新实例时都创建的所有实例,因此,我们将它们附加到 prototype
而不是用 this
构造函数中的关键字。