调用js apply方法
Call, Apply, and Bind are Javascript functions that you will begin to see as you learn more and more about Javascript.
调用,应用和绑定是Javascript函数,随着您对Javascript的了解越来越多,您将开始看到它们。
They are key to being able to manipulate objects in creative and efficient ways. For that reason, they are a must when understanding Javascript.
它们是能够以创造性和高效的方式操纵对象的关键。 因此,在理解Javascript时必须使用它们。
The good news is they are not that hard to understand. It just takes a 5 minute rundown on the 3.
好消息是它们并不难理解。 3号赛车只需要5分钟的时间。
The call method in Javascript is a way to use a method on an object. Rather than giving that object it’s own method, we can use the this keyword to reference any of the keys in that object.
Javascript中的call方法是一种在对象上使用方法的方法。 我们可以使用this关键字引用该对象中的任何键,而不是给该对象自己的方法。
For example:
例如:
let obj = {age: 7}Here we have a normal object with one key value pair. Let’s write a function that will increase the age by any value we want.
在这里,我们有一个带有一对键值对的普通对象。 让我们编写一个函数,该函数将根据需要增加任意值。
let increaseAge = function(a){return this.age = this.age + a}The this keyword in this instance is going to be our object. So you can imagine this object having 100 properties, we can reference any of them. Here we just want to reference the age property.
在这种情况下, this关键字将成为我们的对象。 因此,您可以想象该对象具有100个属性,我们可以引用其中的任何一个。 在这里,我们只想引用age属性。
How do we do this?
我们如何做到这一点?
This is where the call method comes in. We can call call on our increaseAge function. The call method will take 2 methods. The first will be the object we want to call the function on. The second will be any arguments the new function takes.
这就是调用方法的用处。我们可以在我们的gainAge函数上调用call 。 调用方法将采用2种方法。 第一个是我们要调用该函数的对象。 第二个将是新函数采用的所有参数。
increaseAge.call(obj, 5)//obj.age will now have a value of 12So call is used to call functions on objects.
因此, 调用用于调用对象上的函数。
What about Multiple arguments?
多重参数呢?
let increaseAge = function(a, b, c){return this.age = this.age + a + b + c}All we do is follow the object with the 3 arguments
我们要做的就是用3个参数跟随对象
increaseAge.call(obj, 5, 0, 1)//obj.age will now have a value of 13There is a very small difference between apply and call.
apply和call之间的差别很小。
Let’s say instead of writing out each and every argument, we wanted to pass in an array of arguments.
假设不是要写出每个参数,而是要传递一个参数数组。
This is where we use apply.
这是我们使用apply的地方。
First let’s set up our array.
首先,让我们设置数组。
let arr = [5, 5, 5]Now let’s use apply instead of call, and pass our array instead.
现在,我们使用apply而不是call,并传递数组。
increaseAge.apply(obj, arr)//obj.age will now have a value of 22That is the only difference between call and apply.
那是call和apply之间的唯一区别。
Bind is a bit different than both apply and call. But bears the most similarity to call.
绑定与apply和c all都有些不同。 但是拥有最相似的称呼 。
What bind does is it ‘binds’ a function to an object.
绑定的作用是将函数“绑定”到对象。
Using bind will return a function. We can then call this function with the correct arguments to get a result.
使用bind将返回一个函数。 然后,我们可以使用正确的参数调用此函数以获得结果。
Let me clarify this with an example.
让我用一个例子来阐明这一点。
let obj = {age: 5}let addToAge = function(a, b, c){ return this.age = this.age + a + b + c}As we remember, if we call call here we will return the result. If we call bind, we return a function that we can call in the future.
我们记得,如果在此处调用call ,我们将返回结果。 如果调用bind,则返回一个将来可以调用的函数。
To demonstrate this we can console.log our result
为了证明这一点,我们可以console.log我们的结果
console.log(addToAge.bind(obj))//this will log a functionNotice we only passed in the object. This is because we want to be able to call our function with new arguments each time.
注意我们只传入了对象。 这是因为我们希望每次都能使用新的参数调用函数。
So let’s store this new function in a variable so that we can use it later.
因此,让我们将此新函数存储在变量中,以便以后使用。
let bound = addToAge.bind(obj)Now we remember our function takes 3 arguments. We can now use our new variable bound, (a function), and call it with our 3 arguments to get our result.
现在我们记得我们的函数有3个参数。 现在,我们可以使用新的变量bound(一个函数),并使用3个参数调用它以获取结果。
bound(1,2,3)// this will add the age (5) with the 3 arguments (1, 2, 3) to get 11Notice that we did not pass an array in here.
注意,我们没有在此处传递数组。
This is what I meant when I said bind can be more compared to call than apply.
这就是我说绑定可以比调用比应用多得多的意思。
Call: Allows you to create functions that work with properties of an object. We use the this keyword to represent the object that we will pass into call. This allows us to create functions that can be versatile and work with many different objects with the same properties.
调用 :允许您创建与对象属性一起使用的函数。 我们使用this关键字表示将传递给调用的对象。 这使我们能够创建通用的功能,并与具有相同属性的许多不同对象一起使用。
Apply: Apply does the same as call. But instead of passing in arguments one by one, we can pass in an array of items.
Apply :Apply与call相同。 但是我们可以不传递一个参数,而可以传递一个项目数组。
Bind: Bind will ‘bind’ a function to an object and return a new function that we can use at a later time. This allows us to call this new function whenever we want, knowing that the properties of the object are stored and bound to our new function. This allows us to call this function with new arguments any time we want to do something to that object such as increase the age property as we did in our example.
绑定 :绑定会将一个函数“绑定”到一个对象,并返回一个新函数,供以后使用。 这使我们知道对象的属性已存储并绑定到我们的新函数时就可以随时调用此新函数。 这使我们可以在想要对对象做任何事情的时候,使用新的参数调用该函数,例如像我们在示例中那样增加age属性。
As you become more and more advanced in Javascript you will start to noticed functions being used to interact with objects in these ways.
随着您对Java语言的了解越来越高,您将开始注意到用于以这些方式与对象进行交互的函数。
For example, when memoizing Fibinacci, you will see the call or apply function being used as we call a passed in function when needed.
例如,在记住斐波那契时,您会看到调用或应用函数正在使用,因为我们在需要时调用传递的函数。
Thanks for reading. Please follow me on Twitter @thedrewprint and find me on LinkedIn — Andrew Richards
谢谢阅读。 请在Twitter @thedrewprint上关注我,并在LinkedIn上找到我-Andrew Richards
翻译自: https://medium.com/@arichards4814/call-apply-bind-javascript-methods-7d96a73816e8
调用js apply方法
相关资源:微信小程序源码-合集6.rar