Fab black ore pool
In JavaScript, these three methods are used to change the direction of this object of function
are all used to change the direction of this object of a function
the first parameter is the object to be pointed to by this
can continue to pass parameters
< H2 > differences:call and apply methods are direct calls to a function, but the bind () method needs to add () to execute
varxb={
name:' Xiaobing< br />gender:' Female< br /> say:function (){
alert(this.name+', 39;+ this.gender);< br />}
}varother={
name:' Xiaodong< br />gender:' Male< br />}
xb.say();===& gt; Results Xiaobing, female < / pre >can be used simply:
< pre t = "code" L = "JS" > 1. XB. Say. Call (other)< br />2.xb.say.apply(other);< br />3.xb.say.bind(other)(); Pre > < H2 > parameter transfer: < pre t = "code" L = "JS" > varxb = {
Name: 39; Xiaobing< br />gender:' Female< br /> say:function (school,grade){
alert(this.name+', 39;+ this.gender+'---- 39;+ school+', 39;+ grade);< br />}
}varother={
name:' Xiaodong< br />gender:' Male
} < / pre >call() to transfer parameters:
< pre t = "code" L = "JS" > xb.say.call (other, & # 39; Stanford 39; 3')< br />//====> Xiao Dong, male --- Stanford, 3 < / pre >use apply() to transfer parameters:
< pre t = "code" L = "JS" > xb.say.apply (other, [ 39; sitanfu', 39; third']);< br />//====> Xiaodong, male ---- sitanfu, third < / pre >the usage of call() and apply() functions is actually the same, but the form of parameter transfer is different
use bind() to transfer parameters:
< pre t = "code" L = "JS" > xb.say.bind (other, &; Stanford 39; 3');
/ / is it similar to call parameter passing < / pre >but xb.say.bind (other,'stanford ',Ɖ') above returns a function that is not executed
console.log(xb.say.bind(other));
/ / xb.say.bind (other) can be used; Print it out to see
/ = = = = & gt; What is returned is a function, which is returned in the next section of code
} < / pre >return result:
< pre t = "code" L = "JS" > function (school, grade) {
alert (this. Name + ') 39;+ this.gender+'---- 39;+ school+', 39;+ grade); 12 < / pre >so to execute, you need to add () after xb.say.bind (other) to let it execute. The results are as follows:
< pre t = "code" L = "JS" > console.log (XB. Say. Bind (other)) (# 39; Stanford 39; 3');< br />//==> Xiao Dong, male --- Stanford, 3 < / pre >source code implementation of additional bind:
< pre t = "code" L = "JS" > / / the. Bindmethod from prototype. JS
function. Prototype. Bind = function() {
varfn = this, args = array. Prototype. Slice. Call (arguments), object = args. Shift()< br />returnfunction(){
returnfn.apply(object,args.concat(Array.prototype.slice.call(arguments)));< br />}
}
Call (thisobj [, args]) and apply (thisobj [, args])
both have the same functions. In short, they are to change the this point in the object currently using this method, The difference between the two (the first parameter is the same) is that the parameters passed in the call method are listed one by one, while the second parameter in the apply method is an array
or is the illustration more intuitive:
< br />
1
2
3
4
5
6
7
8
9
10
window.color=' red';< br />var o={color:" blue"};< br />function sayColor(){
alert(this.color);< br />};< br />sayColor(); // Red (global function, this is window)
saycolor. Call (this); red (global function, this is window)
saycolor. Call (this); red (global function, this is window)
saycolor// Red (call method is called and the specified object is this, here this is window, which is meaningless)
saycolor.call (window)// Red (call method is called and the specified object is window, which is meaningless)
saycolor. Call (o))// Blue (call method is called to specify that the object is O, so this refers to the object o, where the original window points to o)
saycolor. Apply (o)// Blue (call method is called to specify that the object is O, so this refers to the object o, where the original window points to o)
the bind() method in ecmascript5 is similar to the former two methods. Bind() creates an instance of a function, and the value of this instance is bound to the value passed to the bind() function
example:
< br />
1
2
3
4
5
6
function a(y){
return this.x+y;< br />};< br />var o={x:1};< br />var g=a.bind(o);< br />g(2);// 3
from the example, we can see that function a is bound to object o and returns a new function g. when G is called, function a will call
bind() as a method of object O. this method binds the function to an object and returns a new function. All the parameters in the new function will be passed into the bound function
let's see their differences
in JS, these three are used to change the direction of this object of function. What's the difference between them
before we talk about the differences, let's summarize the similarities of the three:
1. They are all used to change the direction of this object of the function
2. The first parameter is the object this points to
3. You can use subsequent parameters to transfer parameters
What's the difference between them? Let's take an example< br /> var xw = {
name : " Xiao Wang< br /> gender : " Male< br /> age : 24,
say : function() {
alert(this.name + " , & quot; + this.gender + " , This year & quot+ this.age); < br /> }
}
var xh = {
name : " Xiao Hong< br /> gender : " Female< br /> age : 18
}
xw.say();
there's nothing to say about it. It must be Xiao Wang, male, 24 years old
how to use the say method of XW to display the data of XH
for call, you can the code as follows:
xw.say.call (XH)
for apply, you can the code as follows:
xw.say.apply (XH)< For bind,
the code as follows:
XW. Say. Bind (XH) ()
if you write xw.say.bind (XH) directly, there will be no result. Do you see the difference? Call and apply are direct calls to functions, while the bind method still returns a function, so you need () to call later
What's the difference between call and apply? Let's rewrite the example a little bit< br />
?< br />
1
2
3
4
5
6
7
8
9
10
11
12
13
var xw = {
name : " Xiao Wang< br /> gender : " Male< br /> age : 24,
say : function(school,grade) {
alert(this.name + " , & quot; + this.gender + " , This year & quot+ this.age + " , In & quot+ school + " On & quot+ grade); < br /> }
}
var xh = {
name : " Xiao Hong< br /> gender : " Female
age: 18
}
you can see that the say method has two more parameters. We pass the parameters through the call / apply parameters< For call,
the code is as follows:
xw.say.call (XH, & quot; Experimental Primary School& quot; Grade six & quot;)<
for apply, the code is as follows:
XW. Say. Apply (XH, [& quot; Experimental Primary School& quot; Zhengzhou psoriasis hospital, grade 6
see the difference? The parameters after call are one-to-one corresponding to the say method, while the second parameter of apply is an array, and the elements in the array are one-to-one corresponding to the say method. This is the biggest difference between the two
How can bind pass parameters? It can pass parameters like call
the code is as follows:
xw.say.bind (XH, & quot; Experimental Primary School& quot; Grade six ()
but since bind still returns a function, we can pass parameters when calling
the code is as follows:
xw.say.bind (XH) (& quot; Experimental Primary School& quot; Grade six & quot;)<
the above is the whole content of this article. I hope you can like and
articles you may be interested in:
comprehensive analysis of apply, call and bind in JavaScript
in-depth analysis of apply, call and bind in JS
methods of changing this direction in JS (call, apply and bind)
talking about call (), apply (), and bind in JavaScript The usage of bind() is
learn from me about call(), apply(), bind() and callback of JavaScript
open the usage mode of apply, call and bind in JavaScript
comparative analysis of the usage of call, apply and bind in JavaScript
JS apply / call / caller / callee / bind usage and difference analysis
clever use of apply, call and bind in JavaScript
the real use of apply() and call() is to expand the scope of the function. The two parameters are not the same.
apply() receives two parameters, one is the scope of the function, the other is the parameter array. The first parameter of the call() method is the same as that of the apply() method, but the parameters passed to the function must be enumerated. For example,
function sum (num1, num2) {
return num1 + num2< br />}
console.log(sum.call(window, 10, 10)); // 20
console.log(sum.apply(window,[10,20])); // 30
bind() has nothing to do with the above two methods. It is a binding function
apply and call
< / blockquote >apply have the same function as call. The difference lies in the method parameter
< pre t = "code" L = "JS" > / / apply. The second parameter is an arrayfunc. Apply (thisval, [arg1, arg2,...])
/ / call starts from the second parameter and is in the form of parameter list
func. Call (thisval, arg1, arg2,...)
/ / use an example to describe that the
/ / math.max function can return the maximum value of the parameter
varmax1 = math.max (6,3,8,5)
/ / if you want to find the maximum value in the array, apply is useful, because math.max does not support passing in array parameters
/ / the apply method can find the maximum value in the array
vararray = [6,3,8,5]< br />varmax2=Math.max.apply(null,array);
/ / the call method is similar to the original method, except that there is an additional thisval,
/ / because this is not used in the max function, the thisval here can pass any value
varmax3 = math.max.call (null, 6,3,8,5) Pre > < blockquote >
bind
1. The call to apply and call will directly return the execution result of the function, while bind will return a function
2. The function requires the input parameter to be the original function parameter minus the parameter passed by bind (regardless of thisval)
func.bind (thisval, arg1, arg2,...)
/ / examples
functionfunction (a, B, c)
{
console.log (a, B, c))< br />}
vara=1,b=2,c=3;< br />varbindFunc=func.bind(null,a);// Func does not use this, so thisval can pass null
bindfunc (B, c)// Note that you need to pass B and C to
/ / math.max and use bind
varmaxfunc = math.max.bind (null, 6,3)< br />varmax4=maxFunc(8,5); pre>
< < < forest > > > is the skill of the <
1, the upper Empress 2 minutes 11 seconds ago. P >
2, the angle of the ascension, the empress of the small P >
3, it is cross cutting right and left, and is a quick cutting equipment. It is a time tool. 100 yen, 100 pieces of furniture, furniture (common goods), bees (beetles), and beetles (Insecta)
4, there is an elephant, a white leaf, and a pair of bamboo leaves. P >
< < forest > > < technical > < / > >
< < forest > >. 4. There is no possibility of being able to lie on the top of the tree, impossible to see the neighborhood of the neighborhood, or to the crowd. He died of one thousand centimeter. P
in the air accidental Association in the air, and the Pachinko (small pearl) came out. The pavilion's pet is still falling in the cliff. The catch of the pet for the family is a balloon. P >
it must be very painful if you don't understand it. If you have great enthusiasm, then everything can be overcome. There are strategies on the Internet
otherwise, I suggest you forget it
The game was set as a social simulation game, and later it was loved by the players for its high degree of freedom. In the game, the player plays the human role of a "villager" and lives in an anthropomorphic animal village. The village occasionally holds various activities, such as planting flowers and plants, exchanging letters and gifts with villagers, etc. The most important point is that the time in the game is completely synchronized with the real time
and the most acclaimed degree of freedom in the game is that there is no task constraint and no time limit, you are allowed not to make friends with your neighbors, you can also fish without farming, and the game has no hunger value and no amount of blood. You don't need to worry about living like other survival games. You can spend the day even if you don't do anything. It's like a paradise. Although such a mechanism is not novel now, it was definitely a bold idea at that time
the game includes weather, season and day night systems. In different weather and seasons, there will be different random events (full of Nintendo's unique Mystery). Because time and reality are synchronized, there will be different activities in specific festivals, such as Christmas and Halloween. But if you miss it, you have to wait until next year The earliest version can travel through time and space by adjusting machine time.)
like many famous Nintendo IP, even though many sequels have been released, the game still maintains the earliest core playing method, and continuously optimizes the player's experience on this basis
forest of forest, this year's gathering! On March 20, 2020 P < < > > forest of interest (date Edition: Animal forest, animal crossing) is an introction to Nintendo's series of events. The system of the columns is called the release rate, and the amount of large amount is used in the desk. P >
series share of the four articles, the forest of the forest (N64 plateau, 2004 transplant NGC), the Mori forest (Nintendo DS flatland), the forest of Expedition: the castle city (Wii plateau), come back! Cherry forest (3DS). A series of four hundred and fifty thousand cloths P >