首页 > 其他 > 详细

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

时间:2019-06-22 11:34:01      阅读:118      评论:0      收藏:0      [点我收藏+]
// 只要函数创建,就有一个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

  

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!