new 生成實例的過程是:this
1.建立一個對象,該對象繼承做爲class的function的原型。spa
2,執行做爲class的function,並改變上下文 this.prototype
3,若是執行的function return Object 則返回function 返回的Object,不然,則生成一個新的對象(繼承於該function原型);code
//本身編的對象
1 var new2 = function(fn) { 2 var k = function(){return {}}; 3 var kk =fn.call(k()); 4 if(typeof kk !=='object' ){ 5 kk = k(); 6 }; 7 kk.__proto__ = fn.prototype; 8 kk.__proto__.constructor = fn; 9 return kk; 10 11 } 12 13 function lala(){ 14 this.name = 'ana'; 15 return {}; 16 }; 17 18 19 var m = new2 (lala); 20 21 22 m instanceof lala; //true 23 24 m.__proto__.constructor ==lala; //true 25 26 m instanceof Object; //true 27 28 29 lala.prototype.say =function() { 30 31 return 'ha '; 32 } 33 34 m.say() // "ha "