zoukankan      html  css  js  c++  java
  • 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

      

  • 相关阅读:
    php的错误处理机制
    php的自动加载函数spl_autoload_register和__autoload
    mysql日志配置
    01,易语言网页填表
    mac 的全文搜索
    react native 热更新
    码云的使用教程
    autolayout实现水平三等分
    mac命令行快捷键
    什么是延展性(Malleability,可鍛性)
  • 原文地址:https://www.cnblogs.com/zhangchs/p/11067664.html
Copyright © 2011-2022 走看看