1、将父对象作为新对象的原型
function object(o) { var n; function F() {} F.prototype = o; n = new F(); n.uber = o; return n; } var shape = { name: 'Shape', toString: function(){ if(this.uber) return this.uber.toString() + ', ' + this.name; else return this.name; } } var twoDShape = object(shape); twoDShape.name = "2 D Shape"; alert(twoDShape.toString());
function Shape(id) { this.id = id; } Shape.prototype.name = 'shape'; Shape.prototype.toString = function(){return this.name;}; function Triangle() { Shape.apply(this, arguments);//原生属性继承 }
原文:http://blog.csdn.net/zhengwei223/article/details/42144189