-
Object.create(proto,[propertiesObject]): 返回一个新对象,带有指定的原型对象和属性;
// Shape - 父类(superclass) function Shape() { this.x = 0; this.y = 0; } // 父类的方法 Shape.prototype.move = function(x, y) { this.x += x; this.y += y; console.info('Shape moved.'); }; // Rectangle - 子类(subclass) function Rectangle() { Shape.call(this); // call super constructor. } // 子类续承父类 Rectangle.prototype = Object.create(Shape.prototype); Rectangle.prototype.constructor = Rectangle; var rect = new Rectangle(); console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle); // true console.log('Is rect an instance of Shape?', rect instanceof Shape); // true rect.move(1, 1); // Outputs, 'Shape moved.'
-
Object.setPrototypeOf() : 方法设置一个指定的对象的原型 ( 即, 内部[[Prototype]]属性)到另一个对象或
null
setPortoTypeOf方法就比较好理解了,其实就等价于
Object.prototype.__proto__