模擬new的過程

        function NewObj () {
            var Constructor = [].shift.call(arguments);
            var obj = Object.create ? Object.create(null) || {}; //建立空對象,Object.create第一個參數傳入null,建立無原型的對象app

            obj.__proto__ = Constructor.prototype; //設置原型指向prototype

            Constructor.apply(obj, arguments); //調用構造器,給對象設置自身屬性對象

            return obj; //返回該對象
        }原型

相關文章
相關標籤/搜索