[英]JavaScript Inheritance constructor.prototype
I can't understand line 4. 我不明白第4行。
If I write like derive.constructor.prototype
, 如果我写类似
derive.constructor.prototype
,
The error will occur. 将会发生错误。
And also, What is base.apply(derive, baseArgs);
而且,什么是
base.apply(derive, baseArgs);
? ?
I thought that I can do like base(baseArg)
I couldn't do that. 我以为我可以像
base(baseArg)
一样做,但我做不到。
function initializeBase(derive, base, baseArgs) {
base.apply(derive, baseArgs);
for(var prop in base.prototype) {
var proto = derive.constructor.prototype;
if(!proto[prop]) {
proto[prop] = base.prototype[prop];
}
}
}
var Member = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
Member.prototype.getName = function() {
return this.firstName + ' ' + this.lastName;
};
var SpecialMember = function(firstName, lastName, role) {
initializeBase(this, Member, [firstName, lastName]);
this.role = role;
};
SpecialMember.prototype.isAdministrator = function() {
return (this.role == 'Administrator');
};
var mem = new SpecialMember('Kotaro', 'Hayafune', 'Administrator');
document.writeln('Name: ' + mem.getName() + '<br>');
document.writeln('Administrator: ' + mem.isAdministrator());
Not sure what your variables contain here but usually if you want the prototype you would write: 不确定这里的变量包含什么,但是通常如果您想要原型,可以编写:
var proto = derive.prototype;
and if you wanted the constructor you'd write: 如果需要构造函数,则可以编写:
var constr = derive.prototype.constructor;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.