专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

模拟实现JS的apply&call方法 js模拟post

ins518 2024-09-29 18:36:02 技术文章 185 ℃ 0 评论

call() 方法在使用一个指定的 this 值和若干个指定的参数值的前提下调用某个函数或方法。

var foo = {
 value: 1
};
function bar() {
 console.log(this.value);
}
bar.call(foo); // 1

注意两点:

  • call 改变了 this 的指向,指向到 foo
  • bar 函数执行了

模拟实现第一步

那么我们该怎么模拟实现这两个效果呢?

试想当调用 call 的时候,把 foo 对象改造成如下:

var foo = {
 value: 1,
 bar: function() {
 console.log(this.value)
 }
};
foo.bar(); // 1

所以我们模拟的步骤可以分为:

  • 将函数设为对象的属性
  • 执行该函数
  • 删除该函数
// 第一版
Function.prototype.call2 = function(context) {
 // 首先要获取调用call的函数,用this可以获取
 context.fn = this;
 context.fn();
 delete context.fn;
}
// 测试一下
var foo = {
 value: 1
};
function bar() {
 console.log(this.value);
}
bar.call2(foo); // 1

模拟实现第二步

call 函数还能给定参数执行函数

var foo = {
 value: 1
};
function bar(name, age) {
 console.log(name)
 console.log(age)
 console.log(this.value);
}
bar.call(foo, 'kevin', 18);
// kevin
// 18
// 1

注意:传入的参数并不确定

我们可以从 Arguments 对象中取值,取出第二个到最后一个参数,然后放到一个数组里。

// 以上个例子为例,此时的arguments为:
// arguments = {
// 0: foo,
// 1: 'kevin',
// 2: 18,
// length: 3
// }
// 因为arguments是类数组对象,所以可以用for循环
var args = [];
for(var i = 1, len = arguments.length; i < len; i++) {
 // args.push('arguments[' + i + ']');
 args[i-1] = 'arguments[' + i + ']';
}
// 执行后 args为 ["arguments[1]", "arguments[2]", "arguments[3]"]

不定长的参数问题解决了,我们接着要把这个参数数组放到要执行的函数的参数里面去。

// 将数组里的元素作为多个参数放进函数的形参里
context.fn(args.join(','))
// (O_o)??
// 这个方法肯定是不行的啦!!!

也许有人想到用 ES6 的方法,不过 call 是 ES3 的方法,我们为了模拟实现一个 ES3 的方法,要用到ES6的方法,好像……,嗯,也可以啦。但是我们这次用 eval 方法拼成一个函数,类似于这样:

eval('context.fn(' + args +')')

这里 args 会自动调用 Array.toString() 这个方法。

所以我们的第二版克服了两个大问题,代码如下:

// 第二版
Function.prototype.call2 = function(context) {
 context.fn = this;
 var args = [];
 for(var i = 1, len = arguments.length; i < len; i++) {
 // args.push('arguments[' + i + ']');
 args[i - 1] = 'arguments[' + i + ']';
 }
 eval('context.fn(' + args +')');
 delete context.fn;
}
// 测试一下
var foo = {
 value: 1
};
function bar(name, age) {
 console.log(name)
 console.log(age)
 console.log(this.value);
}
bar.call2(foo, 'kevin', 18); 
// kevin
// 18
// 1

模拟实现第三步

模拟代码已经完成 80%,还有两个小点要注意:

1.this 参数可以传 null,当为 null 的时候,视为指向 window

var value = 1;
function bar() {
 console.log(this.value);
}
bar.call(null); // 1

2.函数是可以有返回值的!

var obj = {
 value: 1
}
function bar(name, age) {
 return {
 value: this.value,
 name: name,
 age: age
 }
}
console.log(bar.call(obj, 'kevin', 18));
// Object {
// value: 1,
// name: 'kevin',
// age: 18
// }
// 第三版
Function.prototype.call2 = function (context) {
 var context = context || window;
 context.fn = this;
 var args = [];
 for(var i = 1, len = arguments.length; i < len; i++) {
 // args.push('arguments[' + i + ']');
 args[i - 1] = 'arguments[' + i + ']';
 }
 var result = eval('context.fn(' + args +')');
 // var result = new Function('context', 'arguments', 'return context.fn(' + args + ')')(context, arguments); 
 // 上面使用new Function相当于var binderFn = new Function('context', 'arguments', 'return context.fn(' + args + ')');
 // var result = binderFn(context, arguments);
 // args = ['arguments[0]','arguments[1]', 'arguments[2]']...
 delete context.fn
 return result;
}
// 测试一下
var value = 2;
var obj = {
 value: 1
}
function bar(name, age) {
 console.log(this.value);
 return {
 value: this.value,
 name: name,
 age: age
 }
}
bar.call2(null); // 2
console.log(bar.call2(obj, 'kevin', 18));
// 1
// Object {
// value: 1,
// name: 'kevin',
// age: 18
// }

apply的模拟实现

Function.prototype.apply = function (context, arr) {
 // var context = Object(context) || window;
 // Object(null) or Object(undefined) 都是{};故这个地方不能用Object(context),否则在没有参数的时候结果不正确
 var context = context || window;
 context.fn = this;
 var result;
 if (!arr) {
 result = context.fn();
 }
 else {
 var args = [];
 for (var i = 0, len = arr.length; i < len; i++) {
 args.push('arr[' + i + ']');
 }
 result = eval('context.fn(' + args + ')')
 }
 delete context.fn
 return result;
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表