使用Object.create()进行JavaScript继承?

时间:2010-06-20 16:01:48

标签: javascript object inheritance object-create

如何继承Object.create()?我试过这些,但没有一个正在运作:

var B = function() {};
var A = function() {};
A = Object.create(B);
A.prototype.C = function() {};

var B = function() {};
var A = function() {};
A.prototype.C = function() {};
A = Object.create(B);

var B = function() {};
A = Object.create(B);
var A = function() {};
A.prototype.C = function() {};

没有任何效果。我该如何使用这个新的Object.create() - 函数?

7 个答案:

答案 0 :(得分:27)

在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

构造函数窃取。如果需要调用超类型构造函数,则使用:

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

答案 1 :(得分: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'

只是为了确保b不仅仅是a的克隆,

a.x = 'goodbye';
delete b.x;
b.say(); //alerts 'goodbye'

答案 2 :(得分:16)

我使用的模式是将每个类型包装在一个模块中,并公开createprototype属性,如下所示:

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;
})();

然后我可以像这样构建派生类型:

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; 

})();

使用此模式,每种类型都有自己的create()函数。

答案 3 :(得分:0)

Douglas'Object.create的原始文档在http://javascript.crockford.com/prototypal.html。确保已包含方法的定义

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

答案 4 :(得分:0)

您可以自己定义Object.create,但如果它不是本机的,您将不得不处理它在用于对象的每个for循环中的枚举。

到目前为止,只有新的webkits-Safari5和Chrome本身支持它。

答案 5 :(得分:0)

您可以在Mozilla开发中心找到有关JavaScript inheritance的有用信息。

答案 6 :(得分:0)

好吧,这已经很晚了,但对于其他任何绊脚石的人来说都是如此。您可以在FF和Chrome中使用Object.assign。

在此示例中,使用create制作多维数据集。第一个Object.create(this)使用z属性创建对象,然后使用Object.assign(obj,Square.create(x,y))它将调用Square.create并返回并将其追加到存储在obj中的Cube中

 var Square = {
        x: 0,
        y: 0,

        create: function(x,y) {
            var obj = Object.create(this);
            obj.x = x;
            obj.y = y;
            return obj;
        }
    };

 var Cube = {

        z: 0,

        create:function(x,y,z) {
            var obj = Object.create(this);
            Object.assign(obj, Square.create(x,y)); // assign(target,sources...)
            obj.z = z;
            return obj;
        }
    };

// Your code
var MyCube = Cube.create(20,30,40);
console.log(MyCube);