Home Web Front-end JS Tutorial Learn detailed explanation and working principle of this example in javaScript_Basic knowledge

Learn detailed explanation and working principle of this example in javaScript_Basic knowledge

May 16, 2016 pm 05:04 PM
working principle

How this works

If a function is called as a method of an object, then this will be assigned to the object.

Copy code The code is as follows:

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.

Copy code The code is as follows:

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.

Copy code The code is as follows:

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.

Copy code The code is as follows:

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.

Copy code The code is as follows:

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/

Copy code The code is as follows:

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.

Copy code The code is as follows:

function bound () {
return function () {
console.log(this);
}.bind(this);
}

bound()();
// <- Window
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is SOL coin? How does SOL coin work? What is SOL coin? How does SOL coin work? Mar 16, 2024 am 10:37 AM

What is SOL coin? How does SOL coin work?

What is the architecture and working principle of Spring Data JPA? What is the architecture and working principle of Spring Data JPA? Apr 17, 2024 pm 02:48 PM

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

What is Polygon coin? How does Polygon coin work? What is Polygon coin? How does Polygon coin work? Mar 16, 2024 am 09:22 AM

What is Polygon coin? How does Polygon coin work?

What is VET coin? How does VET coin work? What is VET coin? How does VET coin work? Mar 16, 2024 am 11:40 AM

What is VET coin? How does VET coin work?

What is SHIB coin? How does SHIB coin work? What is SHIB coin? How does SHIB coin work? Mar 17, 2024 am 08:49 AM

What is SHIB coin? How does SHIB coin work?

What is Algorand coin? How does Algorand coin work? What is Algorand coin? How does Algorand coin work? Mar 17, 2024 am 08:30 AM

What is Algorand coin? How does Algorand coin work?

What is Beam Coin? How does Beam Coin work? What is Beam Coin? How does Beam Coin work? Mar 15, 2024 pm 09:50 PM

What is Beam Coin? How does Beam Coin work?

What is AR coin? How does AR coin work? What is AR coin? How does AR coin work? Mar 15, 2024 pm 07:25 PM

What is AR coin? How does AR coin work?

See all articles