Home >Web Front-end >JS Tutorial >In-depth analysis of this keyword in Javascript_javascript skills
First of all, let me throw out a conclusion: "In Javascript, the This keyword always points to the owner of the function (method) ".
Function
As mentioned in my previous article (Javascript scope), for functions defined globally, the owner of the function is the current page, which is the window object.
This is why I put the function in quotes, because the function defined globally is actually a method of the window object.
So, we can call it directly through the function name, or call it through the window. method name. At this time, the this keyword in the method points to its owner: the window object.
If we look at the introduce attribute of window, we will get:
The answer is yes, if you call the introduce function, you will know me as Laruence.
Event handler function
Perhaps, most of the confusion about this keyword comes from using functions (methods) in event processing.
function showValue() {
alert(this.value);
}
document.getElementById('name').onclick = showValue;
Haha, if you can think of this question, it means you are reading my article seriously. Otherwise, I suggest you read it from the beginning, otherwise you will still be confused after reading it~
Well, yes, for the above code, showValue is defined in the global object, so it seems that the problem can only occur when the onclick event is bound.
We know that everything in JS is an object, and functions and methods are also properties of objects, except that functions have executable internal properties. Therefore, for the above code, when binding the processor to onclick, it actually assigns a value to the onclick attribute of the input box Dom object with the id of name.
In other words, we give the function showValue Copy to the onclick attribute of the name input box object. If we look at the onclick at this time:
However, confusing things come, such as the following writing:
Well, Because at this time, it is not an assignment, but a reference.
If we pay attention to the two ways of writing onclick, you will find that for the previous method, we used:
dom.onclick = showvalue; //No caller
As for the method just now:
onclick = "showvalue()" //There is a caller
This can also reflect the difference between the two: for the former, it is assignment, while for the latter, it is reference. If we look at the onclick attribute of the input box at this time, we get:
alert(dom.onclick);
See the difference? You can understand why, right?
Speaking of this, there is a very interesting example. You can try it under IE:
Change the pointing of this
So, now that we know why, how can we make this point to where we want it to point?
For the above event processing function, we can write it in the following ways:
dom.onclick = showValue();
dom.onclick = function() { alert(this.value) ;}
//Think about our quote just now and how to embed this sentence.
dom.addEventListener(dom, showValue, false); //ff only
For situations that are not event handling functions, we can use apply or call to change the pointer of this keyword.
For example:
function introduce() {
alert(this.name);
}
introduce.call(laruence);