// new 通过构造函数返回一个对象
var _new = function(){
var obj = new Object();
Constructor = [].shift.call(arguments);
obj._proto_ = Constructor.prototype;
var ret = Constructor.apply(obj, arguments);
return typeof ret === 'object' ? ret : obj;
}
// call 指定this的值并给某个函数赋参数值进行调用
Function.prototype.call2 = function (context){
// this设置默认值为window
var context = context || window;
context.fn = this;
var args = [];
for (var i=1, len=arguments.length; i<len; i++){
args.push('arguments['+i+']');
}
var result = eval('context.fn('+args+')');
//防止函数有返回值,导致提前结束
delete context.fn;
return result;
}
var foo = {
value: 1
};
function bar(name,age){
console.log(name);
console.log(age);
console.log(this.value);
}
bar.call2(foo,'DG',12);
console