Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

关于JavaScript原型,和underscore.js原码问题

问题描述

var Ctor = function (){};

var baseCreate = function (prototype) {
        if(!_.isObject(prototype)) {
            return {};
        }
        if(nativeCreate) {
            return nativeCreate(prototype);
        }
        Ctor.prototype = prototype;
        var result = new Ctor;
        Ctor.prototype = null;
        return result;
    };

(Ctor.prototype = prototype)使用原型继承再(var result = new Ctor)实例化,最后(Ctor.prototype = null)为什么要清空在原型上添加的方法。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

因为它是全局变量可能多次使用啊,下次使用前肯定要恢复初始状态


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...