具有简明原型分配语法的Javascript继承
我使用这种(具有简明原型分配语法的Javascript继承,javascript,oop,inheritance,Javascript,Oop,Inheritance,我使用这种(prototype)方法定义了两个javascript类: 函数父级(){ 这是a=1; } Parent.prototype={ 您好:函数(){ 返回“你好,我是家长!”; }, setA:功能(a){ 这个a=a; } }; 及 函数子级(){ 这是b=2; } Child.prototype=新父级(); Child.prototype.constructor=Child; Child.prototype={ 您好:函数(){ 返回“你好,我是孩子!”; }, setB:功
prototype
)方法定义了两个javascript类:
函数父级(){
这是a=1;
}
Parent.prototype={
您好:函数(){
返回“你好,我是家长!”;
},
setA:功能(a){
这个a=a;
}
};
及
函数子级(){
这是b=2;
}
Child.prototype=新父级();
Child.prototype.constructor=Child;
Child.prototype={
您好:函数(){
返回“你好,我是孩子!”;
},
setB:功能(b){
这个.b=b;
}
};
我之所以使用这种技术,是因为我认为它的标准语法过于冗长和稀疏:
Child.prototype.hello=function(){…};
Child.prototype.setB=函数(b){…};
这里的问题是,我正在覆盖Child.prototype
(从父级继承而来)丢失.setA()
方法(但正确地覆盖了.hello()
)
合并两个原型是解决方案吗?怎么做?
这种方法会导致问题吗
合并两个原型是解决方案吗
对
怎么做
只需编写一个循环,在对象文本上运行,并将每个属性合并到原型中
function inherit(Child, Parent, methods) {
var p = Child.prototype = Object.create(Parent.prototype);
p.constructor = Child;
for (var m in methods)
p[m] = methods[m];
return p;
}
合并两个原型是解决方案吗
对
怎么做
只需编写一个循环,在对象文本上运行,并将每个属性合并到原型中
function inherit(Child, Parent, methods) {
var p = Child.prototype = Object.create(Parent.prototype);
p.constructor = Child;
for (var m in methods)
p[m] = methods[m];
return p;
}
下面是我从pro javascript设计模式复制的一个示例
function extend (subClass, superClass) {
var F = function () {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if (superClass.prototype.constructor === Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
} // end if
} // end extend()
function Person (name) {
this.name = name;
} // end Person()
Person.prototype.getName = function () {
return this.name;
}; // end getName()
function Author (name, books) {
Author.superclass.constructor.call(this, name);
this.books = books;
} // end Author()
extend(Author, Person);
Author.prototype.getBooks = function () {
return this.books;
}; // end getBooks()
Author.prototype.getName = function () {
var name = Author.superclass.getName.call(this);
return name + " , author of " + this.getBooks();
}; // end getName()
var a = new Author("xxx", "xxx");
console.log(a.getName()); // xxx , author of xxx
作者列举了实现继承的三种方法:基于类、基于原型和基于扩充
您可以阅读代码,也可以阅读这本书。这里是我从pro javascript设计模式复制的一个示例
function extend (subClass, superClass) {
var F = function () {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if (superClass.prototype.constructor === Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
} // end if
} // end extend()
function Person (name) {
this.name = name;
} // end Person()
Person.prototype.getName = function () {
return this.name;
}; // end getName()
function Author (name, books) {
Author.superclass.constructor.call(this, name);
this.books = books;
} // end Author()
extend(Author, Person);
Author.prototype.getBooks = function () {
return this.books;
}; // end getBooks()
Author.prototype.getName = function () {
var name = Author.superclass.getName.call(this);
return name + " , author of " + this.getBooks();
}; // end getName()
var a = new Author("xxx", "xxx");
console.log(a.getName()); // xxx , author of xxx
作者列举了实现继承的三种方法:基于类、基于原型和基于扩充
您可以阅读代码,也可以阅读这本书。考虑以下继承:Child.prototype=Object.create(new Parent())
@Matías您可以使用Object.create或helper函数在设置继承的原型部分时阻止创建父实例,因此它应该是:Child.prototype=Object.create(Parent.prototype)
您和OP的操作方式父对象的每个实例特定成员this.someting
都放在子原型上,并且(如果您操作正确)在创建子实例(父对象中的Parent.apply(此,参数))时立即隐藏。或者更糟;程序员假设子对象有父实例成员,却没有意识到它们实际上是共享的。code>@Matías您可以使用Object.create或helper函数在设置继承的原型部分时阻止创建父实例,因此它应该是:Child.prototype=Object.create(Parent.prototype)
您和OP的操作方式父对象的每个实例特定成员this.someting
都放在子原型上,并且(如果您操作正确)在创建子实例(父对象中的Parent.apply(此,参数))时立即隐藏。或者更糟;程序员假定子实例有父实例成员,但没有意识到它们实际上是共享的。为了完整性,如果子实例希望有this.a
赋值为1或以某种方式重新使用父初始化代码,那么在子构造函数体中,您应该有Parent.apply(这是参数)代码>这部分经常被遗忘,OP的示例代码不能保证需要它,但我甚至看到Crockford抱怨在继承时父代码没有被重用,所以值得一提的是它可以被重用。@HMR:谢谢提醒,我忘记了无意识复制粘贴构造函数的时候。当然应该在那里!为了完整性,如果子实例希望将this.a
赋值为1或以某种方式重新使用父初始化代码,那么在子构造函数主体中,您应该有Parent.apply(这是参数)代码>这部分经常被遗忘,OP的示例代码不能保证需要它,但我甚至看到Crockford抱怨在继承时父代码没有被重用,所以值得一提的是它可以被重用。@HMR:谢谢提醒,我忘记了无意识复制粘贴构造函数的时候。当然应该在那里!