如何在Javascript中实现应用模式 [英] How to implement apply pattern in Javascript

查看:124
本文介绍了如何在Javascript中实现应用模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中引用函数调用模式应用调用模式是什么,如何使用?
使用这种调用模式有什么好处。

What is apply invocation pattern in Javascript in reference to function invocation patterns and how can I use it? What are the benefits of using this invocation pattern.

推荐答案

使用 / code>与函数上下文(这个关键字)和参数传递相关。

The use of apply is related to the function context (the this keyword) and argument passing.

我认为你应该知道这个关键字在哪些情况下 设置:

First, I think you should know in which cases the this keyword is implicitly set:

1-当一个函数被调用为一个方法(该函数被调用为对象的成员)时:

1- When a function is called as a method (the function is invoked as member of an object):

obj.method(); // 'this' inside method will refer to obj

2-一个正常的函数调用:

2- A normal function call:

myFunction(); // 'this' inside the function will refer to the Global object
// or 
(function () {})();

3-当使用

var obj = new MyObj(); // this will refer to a newly created object.

以下是应用 call 来,这些方法可以让您在调用函数时设置上下文,例如:

Here is when apply and call come, those methods let you set explicitly the context when you invoke a function, eg.:

function test(a) {
  alert(this + a);
}

test.call('Hello', ' world!');

在上述代码中,当测试函数称为将关键字设置为String('Hello'),并传递 参数('world。')。

In the above code, when the test function is called setting the this keyword to a String ('Hello'), and passing the a argument (' world.').

两者,调用应用更改执行函数的上下文(关键字),但它们之间的差异是使用 apply 可以发送一个Array或类似Array的对象作为参数的功能,这是非常有用的,例如:

Both, call and apply change the context of the executing function (the this keyword) inside the called function, but the difference between them is that with apply, you can send an Array or an Array-like object as the arguments of the function to be executed, which is extremely useful, for example:

function sum() {
  var result = 0;
  for (var i = 0; i < arguments.length; i++) {
    result += arguments[i];
  }
  return result;
}

var args = [1,2,3];
sum.apply(null, args); // will return 6

这可以避免一些非常黑客,并且通用) eval 调用如下:

That can avoid some very hacky, bad (and common) eval calls like:

eval('sum(' + args.join() +')');

另一个例子是 Math.max Math.min 方法,这些方法可以接收任意数量的参数,如:

Another example, the Math.max and Math.min methods, those methods can receive an arbitrary number of arguments like:

var max = Math.max(2,4,6,8); // 8

但是,如果你有数组数组,你会怎么样?

But what if you have an Array of numbers?

var numbers = [2,4,6,8];
numbers.push(10);
var max = Math.max.apply(null, numbers); // 10

另请注意,当 null 未定义用作参数与调用应用这个对象将引用Global对象(类似于#2,正常的函数调用)。

Also note that when null or undefined is used as the this argument with call or apply, the this object will refer to the Global object (similar to the case #2, normal function invocation).

对于 Math.max 示例,上下文并不真正相关,因为它们就像 static方法,关键字不在内部使用...

For the Math.max example, the context is not really relevant, since they are just like "static" methods, the this keyword is not used internally...

这篇关于如何在Javascript中实现应用模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆