


Learn detailed explanation and working principle of this example in javaScript_Basic knowledge
How this works
If a function is called as a method of an object, then this will be assigned to the object.
var parent = {
method: function () {
console.log(this);
}
};
parent.method();
// <- parent
Note that this behavior is very "fragile". If you get a reference to a method and call it, the value of this will not be the parent, but the window global object. This confuses most developers.
ThisClownCar();
// <- Window
Change this
.call, .apply and .bind methods are used to operate the method of calling functions and help us define the value of this and the parameter values passed to the function.
Function.prototype.call can have any number of parameters, the first parameter is assigned to this, and the rest are passed to the calling function.
Array.prototype.slice.call([1, 2 , 3], 1, 2)
// <- [2]
Function.prototype.apply behaves similarly to .call, but the parameter it passes to the function is an array instead of Any parameters.
String.prototype.split.apply('13.12.02', ['.'])
// <- ['13', '12', '02']
Function.prototype.bind creates a special function that will always use the parameters passed to .bind as the value of this, and can assign some parameters to create a curried version of the original function.
var arr = [1, 2];
var add = Array.prototype.push.bind(arr, 3);
// effectively the same as arr.push(3)
add();
// effectively the same as arr.push(3, 4)
add(4);
console.log(arr);
// <- [1, 2, 3, 3, 4]
this in the scope chain
In the following example, this will not remain unchanged in the scope chain. This is a flaw in the rules, and often causes confusion for amateur developers.
function scoping () {
console.log( this);
return function () {
console.log(this);
};
}
scoping()();
// <- Window
// <- Window
A common method is to create a local variable to hold a reference to this, and there cannot be a variable with the same name in the child scope. A variable with the same name in the child scope will overwrite the reference to this in the parent scope. http://www.cnblogs.com/sosoft/
function retaining () {
var self = this;
return function () {
console.log(self);
};
}
retaining ()();
// <- Window
Unless you really want to use both this of the parent scope and the current this value, for some inexplicable reason, I prefer to use the .bind function. This can be used to assign this from the parent scope to the child scope.
function bound () {
return function () {
console.log(this);
}.bind(this);
}
bound()();
// <- Window

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What is SOL coin? How does SOL coin work?

What is the architecture and working principle of Spring Data JPA?

What is Polygon coin? How does Polygon coin work?

What is VET coin? How does VET coin work?

What is SHIB coin? How does SHIB coin work?

What is Algorand coin? How does Algorand coin work?

What is Beam Coin? How does Beam Coin work?

What is AR coin? How does AR coin work?
