Crockford が推奨する継承と混同しています。Crockford メソッドと一般的な (既定の) 方法の主な違いは何ですか。
//Crockford method
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
以下はより一般的な方法です
function Base(name) {
this.name = name;
}
Base.prototype.getName = function () {
return 'Base :' + this.name;
}
function Child(name) {
this.name = name;
}
Child.prototype.getName = function () {
return 'Child :' + this.name;
}
function Kid(name) {
this.name = name;
}
Kid.prototype.getName = function () {
return 'Kid :' + this.name;
}
Child.prototype = new Base ("childBase");
Kid.prototype = new Child ("kidChild");
var base = new Base ("myBase");
var child = new Child("myChild");
var kid = new Kid("myKid");
console.log(base.getName());
console.log(child.getName());
console.log(kid.getName());
上記の2つの違いは何ですか?
実は私はクロックフォード法を完全には理解できていません。一般的な方法の欠点とクロックフォード法の利点を理解するのに役立つ人はいますか。