类抄袭集成javascript
function father(){ var instance = this; instance.name = this.name; instance.getName = function(){ return this.name; } return instance ; } function subClass (name){ father.call(subClass); } function classFactroy (name) { return new subClass(name); }
类原型继承java
function father(){}; father.prototype.getName = function(){ return this.name; }; function subClass(name){ this.name = name; } subClass.prototype = new father();
第一种方法的的调用属性无须回溯原型链,效率高,可是每一个实例本地都是一份独立属性拷贝,浪费内存空间;第二种方法的都是公共的指向原型链的属性,本地没有内存开销,可是方法调用须要回溯原型链,效率低下;根据不一样的状况须要分别对待;this
第一种方法没有涉及到任何的原型链维护,没法用instanceof类型判断;prototype