new运算符工作原理(new运算符的伪码实现)

// 只要函数创建,就有一个prototype属性
// 构造函数和普通函数的区别就是调用的时候又没有用 new function Fn() {
  // this 就是实例化后的对象 三段式 var this = { __proto__: Fn.prototype }; this.XXX = XXX; return this; }
// 原型
Fn.prototype = {
  // 浏览器自带
  constructor: Fn()  // 构造函数
__proto__:Object
}; 

obj->Fn.prototype->Object.prototype->null

  

//new运算符的伪码实现  
function _new(clazz, args){  
    //clone(clazz.prototype)  
    var this = {};  
    this.__proto__ = clazz.prototype;  
    var obj  = clazz.apply(this, args);  
    var type = typeof obj;  
    if(type == "object" && obj !== null || type == "function"){  
        return obj;  
    }else{  
        return this;  
    }  
    /* 另一种写法 
    if(obj === null || type == "undefined" || type == "boolean" || type == "number" || type == "string"){ 
        return this; 
    }else{ 
        return obj; 
    } 
    */  
}  
var a = new ClassA(1,2);  
var a = _new(ClassA, [1,2]);  //伪码    

来源:https://wmingjian.iteye.com/blog/1881658

  create read update delete

  create select update delete

  

原文地址:https://www.cnblogs.com/zhangchs/p/11067664.html