Tow*_*wer 15 javascript inheritance object object-create
我如何继承Object.create()?我试过这些,但都没有工作:
var B = function() {};
var A = function() {};
A = Object.create(B);
A.prototype.C = function() {};
Run Code Online (Sandbox Code Playgroud)
和
var B = function() {};
var A = function() {};
A.prototype.C = function() {};
A = Object.create(B);
Run Code Online (Sandbox Code Playgroud)
和
var B = function() {};
A = Object.create(B);
var A = function() {};
A.prototype.C = function() {};
Run Code Online (Sandbox Code Playgroud)
没有任何效果.我该如何使用这个新的Object.create()函数?
Vla*_*den 29
在JavaScript中有几种继承方式
施工继承.如果您不需要调用超类型构造函数,请使用:
function Rectangle(length, width) {
this.length = length;
this.width = width;
}
Rectangle.prototype.getArea = function() {
return this.length * this.width;
};
// inherits from Rectangle
function Square(size) {
this.length = size;
this.width = size;
}
Square.prototype = Object.create(Rectangle.prototype);
var rect = new Rectangle(6, 8);
var square = new Square(10);
console.log(rect.getArea()); // 48
console.log(square.getArea()); // 100
console.log(rect instanceof Rectangle); // true
console.log(rect instanceof Object); // true
console.log(square instanceof Square); // true
console.log(square instanceof Rectangle); // true
console.log(square instanceof Object); // true
Run Code Online (Sandbox Code Playgroud)
构造函数窃取.如果需要调用超类型构造函数,则使用:
function Rectangle(length, width) {
this.length = length;
this.width = width;
}
Rectangle.prototype.getArea = function() {
return this.length * this.width;
};
// inherits from Rectangle
function Square(size) {
Rectangle.call(this, size, size);
}
Square.prototype = Object.create(Rectangle.prototype);
var rect = new Rectangle(6, 8);
var square = new Square(10);
console.log(rect.getArea()); // 48
console.log(square.getArea()); // 100
console.log(rect instanceof Rectangle); // true
console.log(rect instanceof Object); // true
console.log(square instanceof Square); // true
console.log(square instanceof Rectangle); // true
console.log(square instanceof Object); // true
Run Code Online (Sandbox Code Playgroud)
Che*_*try 22
Object.create()用于继承对象,而不是像你想要的那样构造.它几乎创建了一个新对象,旧对象设置为原型父对象.
var A = function() { };
A.prototype.x = 10;
A.prototype.say = function() { alert(this.x) };
var a = new A();
a.say(); //alerts 10
var b = Object.create(a);
b.say(); //alerts 10
b.x = 'hello';
b.say(); //alerts 'hello'
Run Code Online (Sandbox Code Playgroud)
并且只是为了确保b不仅仅是a的克隆,
a.x = 'goodbye';
delete b.x;
b.say(); //alerts 'goodbye'
Run Code Online (Sandbox Code Playgroud)
Sea*_*lan 17
我用于此的模式是将每个类型包装在一个模块中,并公开create和prototype属性,如下所示:
var Vehicle = (function(){
var exports = {};
exports.prototype = {};
exports.prototype.init = function() {
this.mph = 5;
};
exports.prototype.go = function() {
console.log("Going " + this.mph.toString() + " mph.");
};
exports.create = function() {
var ret = Object.create(exports.prototype);
ret.init();
return ret;
};
return exports;
})();
Run Code Online (Sandbox Code Playgroud)
然后我可以像这样构建派生类型:
var Car = (function () {
var exports = {};
exports.prototype = Object.create(Vehicle.prototype);
exports.prototype.init = function() {
Vehicle.prototype.init.apply(this, arguments);
this.wheels = 4;
};
exports.create = function() {
var ret = Object.create(exports.prototype);
ret.init();
return ret;
};
return exports;
})();
Run Code Online (Sandbox Code Playgroud)
使用此模式,每种类型都有自己的create()功能.