In my career, I started using JavaScript for project development very early on. But the focus has always been on back-end development, with few front-end aspects involved. So a phenomenon is: the current front-end knowledge level, to deal with the general project is already enough, but the lack of heat, not in-depth research, so that always talk about to find their half bottle bang when the level. In order to solve this embarrassing situation, determined to study a period of time JavaScript, the following knowledge for my Learning process summary, although to my level may incur laughable fruit, but more looking forward to the truth.
In JavaScript, we call a common method that can be done in the following way:
//Global public Call modefunctiongreetingmorning (){ Alert ("Hello morning!"); } greetingmorning ();//Local Invocation ModevarGreetingworld=function() { Alert ("Hello world!"); } greetingworld (); //Self-invocation mode(function(){ Alert ("Hello chaoyang!"); })();
Then the above is the general three methods of invocation, the first two will be attached on the window object above, the difference is that the first is the direct use of function to declare the way, belongs to the public invocation mode, the second is to use the VAR keyword to declare the way to invoke. In other words, the function declared by the first declaration is no problem, whether it is called before or after the function body. The function declared by the second declaration method can only be called after the function body:
//correctly, you can call thisgreetingmorning ();functiongreetingmorning (){ Alert ("Hello morning!"); } //error, prompt could not find methodGreetingworld ();varGreetingworld=function() { Alert ("Hello world!"); }
The first pit we need to be aware of here is the problem of local variables plus no var keyword:
functionGreetinglucy (){ name="Lucy"; Alert ("Hello"+name); } Greetinglucy ();alert (name);
Here our execution results, for the first time will output: Hello Lucy, the second time will output: Lucy. This is very strange, why the name I wrote to the method inside, is supposed to be a local variable, why still output results? In JavaScript, in the body of a method, this variable defaults to a global variable without the addition of the VAR keyword . This is why the results are output.
So knowing the general method of function execution, let's look at a special way to execute a function.
As mentioned above, we directly use () , the way can start the function of immediate execution. But this kind of execution, in fact, in most cases cannot change the scope of the This keyword, because this is the default point to the Window object has been instantiated, so many times when we need to change the scope of this, we have to introduce the apply, the call keyword and the BIND keyword. Let's start by explaining their basic usage.
First, for the Apply method:
//without parameters, we apply this and we can see that we have changed the scope of this //from the original window object, pointing to the GreetinglucyfunctionGreetinglucy (){ varname="Lucy"; alert ("Hello"+name); }greetinglucy.apply ( This);//with two parametersfunctionGreeting (name, age){ Alert ("Hello"+name+""+ Age+" !"); }greeting.apply ( This,["Lucy", at]);
As you can see, when you use apply, the first parameter must point to the object that needs to be applied, and then multiple arguments need to be written as an array.
Look again at the call method:
functionGreetinglucy () { varname="Lucy"; Alert ("Hello"+name); } Greetinglucy.call ( This);functiongreeting (name,age) { Alert ("Hello"+name+""+ Age+" !"); } Greeting.call ( This,"Lucy", -);
As can be seen, when using call, the first parameter also needs to point to the object being called, after which several parameters can be directly written on the line.
From here we can see that apply and call will change the scope of this, and for more detailed information, I will go to the second section to explain.
Let's look at the bind method.
varColor="Red"; varobj={color:"Blue"}; functionSaycolor (){ Alert ( This. color); } varNW=saycolor.bind (obj);//ExecutionNW ();
What will it output here? This will output "blue". Then why not output red? In fact here, the This keyword has a very important feature, that is: who calls me, I point to who . Because of the invocation of the Bind method, this will cause a change in this direction, from the Window object to a pointer to the Obj object. So this will be done with the color from the Obj object.
As we can see from the above example, the bind method does not cause the function to execute immediately, and it needs to be triggered before it can be executed.
Why does bind not execute immediately? We can execute alert (NW) to see the results returned to us:
functionbound Saycolor () {[native code]}
This indicates that the NW object returned by bind is a function, and it is a function that is not executed, and if we want to execute it, we need to add ();
Well, here's what I understand about the use of the Apply call bind.
In fact, their usage is very basic, but they can change this scope of this feature is shocking. I'll focus on this in the later chapters and try to thoroughly understand it. Please look forward to it.
Front-end see around corners JavaScript Basics: Apply, call, and bind you don't know