Function.prototype.myCall = function (obj) {
var obj = obj || window
//var flag = false, temp
//if (obj.hasOwnProperty('fn')){
// flag = true;
// temp = obj.fn
//}
obj.fn = this
var args = []
for (var i = 1; i < arguments.length; i++)
args.push('arguments[' + i + ']')
var result = eval('obj.fn(' + args + ')')
delete obj.fn
//if (flag)
// obj.fn = temp
return result
}
new
實(shí)現(xiàn)思路:新建空對(duì)象窃肠、讓對(duì)象的 __proto__ 指向函數(shù)的 prototype、執(zhí)行構(gòu)造函數(shù)刷允、返回該對(duì)象
細(xì)節(jié)修正:如果構(gòu)造函數(shù)返回的值是對(duì)象或函數(shù)冤留,則返回構(gòu)造函數(shù)返回的對(duì)象或函數(shù)
function objectFactory() {
var obj = new Object()
var Constructor = Array.prototype.shift.call(arguments)
obj.__proto__ = Constructor.prototyep
var ret = Constructor.apply(obj, arguments)
return (typeof ret === 'object' || typeof ret === 'function') ? ret : obj
}