javascript 中的 new 操作

来源:互联网 发布:android 电量优化 编辑:程序博客网 时间:2024/05/22 05:26

    偶然想起Javascript new 的结果到底是什么的问题,比如:var circle =new Shape("circle" ); 

    于是查了下ECMA-262-5.1标准。关键是这一步:Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.

    因此 不考虑异常情况的话上面的new就相当于:

    var circle = {}; 

    var temcircle = Shape.call(circle,  "circle" ); 

    if (typeof(temcircle)=="object"){
        
return temcircle;
    }
    return circle;