tags: this
this: in the function Simply put, this point in time there is a function call of the decision, who calls this function is the function of this points to who
E.g:
1, normal function is called when
fn() ----> window
2, object calls
var obj = {}
obj.f = fn;
obj.f()
this ----> obj
3, call timers: because the underlying fn not our own hand-written code that calls this method is invoked window
setInterval( fn , 1000)
fn()
this ----> window
4, registration event: The event handler called when the underlying browser and browse to find a box to let box.onclick () event fires
box.onclick = fn;
this ------> box
Note: es6 function of the arrow is not pointing to this problem, so in this function of the arrow come from? Its value depends on the outside of this function itself is not a function of the arrow
In the constructor if you go to a new instance, the new operator of this also creates a
Change the way this points:
By calling context mode, the mode of the calling context, we can make this point to point to who will come
1、call()
Role: 1 will call this function
2. The first argument to the function of this then who will point to who
Parameters: The first argument: context (context), who passed, the function of this points to who
The second argument: an argument passed in accordance with the order parameter transfer function values
Syntax: function .call (context, arguments);
E.g:
function Person(name, age){
this.name = name;
this.age = age;
}
var obj = {type:'human'};
Person.call(obj, 'zs', 18);
console.log(obj);
2、apply()
Role: 1, call this function
2, the first argument of this function who will point to who
Parameters: The first argument: context who passed the function of this points to who
The second argument: an array requires passed arguments stored in array
Syntax: function .apply (context, [arguments, arguments, argument ......])
E.g:
function Person(name, age){
this.name = name;
this.age = age;
}
var obj = {type:'human'};
Person.apply(obj,['ls', 20]);
console.log(obj);
3、bind()
Effect: 1, clone this function, then the function returns clone was out
2, so that the new function of this clone have certain fixed points
Parameters: The first argument: context who passed out of the new Clone function inside of this points to who
The second parameter: the parameter assigned according
Syntax: var = new function function .bind (obj)
E.g:
var obj = {type:1}
function Person(name, age){
this.name = name;
this.age = age;
}
var newfn = Person.bind(obj, 'zl', 47);
newfn();
console.log(obj);
to sum up:
Three points this point changed by the same method:
The first parameter context who passed the function of this points to who
difference:
It calls the function call and apply only bind does not call clone
bind and call transfer parameters are the same, apply the second array pass is required parameters
note:
function fn(){
console.log(this);
}
fn.bind(['a'])();
var newfn = fn.bind(['a']);
var f = new newfn();
Although the bind function was cloned after this point has changed is fixed down, but note that the new more powerful, after using new a new or a new object inside of this point will function;
Recall the way:
the new operator to do what things?
1, create a new object
2, so that this points to the new object
3, the constructor is executed
4, return to this new object
Today, the first on here, welcome everyone mutual exchange of learning.
Let's first briefly describe what .NET, and a simple understanding of C#and one of them. 1.NET Overview .NET is the abbreviation of Microsoft.net, which is a technology based on the Windows platform. ...
1、Jquery Introduction to jQuery local download: Online introduction:http://www.bootcdn.cn/jquery/ Support all css2 selectors Most css3 options Cannot be quoted: this document window All click events a...
Reprinted at: https://blog.51cto.com/chaorenhuifei/1703026...
Assuming that the final exam is taken, the full score of the exam is 10, and five students have participated in the exam and scored 5, 3, 5, 2 and 8 points respectively. Now we need to sort their scor...
Work hard to become a savvy female programmer! During this time, I have contacted AutoResetEvent. After I have a simple understanding, I made a small demo to introduce the basic usage. Welcome everyon...
Single case design pattern Hungry Chinese : A class can only create one object Privatization constructor Create an instance of a class inside the class, and be static Privatization object, invoked thr...
Basic steps for dynamic planning Find out the nature of the optimal solution and characterize its structural features. (Looking for the sub-question structure of the optimal solution) Recursively defi...
A simple understanding of Jena and an example Jena is a city in northern Germany. Some people say that it is "a place that gives me a sense of belonging." This article briefly introduces Jen...
Simple understanding of the dichotomy The mathematical concept of the dichotomy is that for a function y=f(x) that is continuous over the interval {a,b} and satisfies f(a)f(b)<0, by constantly putt...
Initiate a GET request Use directlyaxios('/user')method,axios()Method defaults to GET mode Using the axios.get() method, the parameters are written directly?key=valueForm, multiple use?key1=value1&...