var obj = {
value : 0 ,
increment : function ( ) {
this . value + = 1 ;
}
} ;
obj. increment ( ) ;
this
will refer to the object to which the function belongs, in our case, to obj, and this connection will be established after the function is launched, which is the term late binding .add ( 2 , 3 ) ; //five
this
bound to a global object. This is undoubtedly a language error — the constant binding of this to a global object can destroy its context. This is especially noticeable if you use the function inside the method. Let's look at an example:var value = 500 ; // Global variable
var obj = {
value : 0 ,
increment : function ( ) {
this . value ++;
var innerFunction = function ( ) {
alert ( this . value ) ;
}
innerFunction ( ) ; // Function invocation pattern
}
}
obj. increment ( ) ; // Method invocation pattern
innerFunction
is called using the aforementioned “function call” pattern, so this
bound to a global object. As a result, we get 500.this
variable, but this, in my opinion, is a hack.var value = 500 ; // Global variable
var obj = {
value : 0 ,
increment : function ( ) {
var that = this ;
that. value ++;
var innerFunction = function ( ) {
alert ( that. value ) ;
}
innerFunction ( ) ; // Function invocation pattern
}
}
obj. increment ( ) ;
this
to the object within which the function is called.new
operator is used for this implementation. Apparently, the creators of JS decided not to go far beyond the example, and to implement something similar in the “constructor challenge” pattern ...new
operator right before the call, for example:var Cheese = function ( type ) {
cheeseType = type ;
return cheeseType ;
}
cheddar = new Cheese ( "cheddar" ) ; // Returns an object, not a type.
Cheese
is a functional object (which means it can digest the code), we created a new object by calling a function with new
. this
in this case will refer to the newly created object, and the return
behavior will be changed. Speaking of return. Its use in the "constructor call" has two features:return
does not work, and we get this
this
var obj = {
data : "Hello World"
}
var Func1 = function ( ) {
return obj ;
}
var Func2 = function ( ) {
return "I am a simple type" ;
}
var f1 = new Func1 ( ) ; // f1 is assigned to the object
var f2 = new Func2 ( ) ; // f2 assigned to new object
this
, and assign literals to objects if it were not for one thing: the creators of JavaScript associated one of the key features of the language with the given pattern — creating objects with an arbitrary reference to the prototype ( for more , see English here ). This pattern is unintuitive, moreover, problems often arise with it. The solution to the problem was suggested by Douglas Crockford: you can use the augment object with the create method. I am pleased to announce that since version 1.8.5 of JavaScript, Object.create
is quite a working tool.this
. Due to the fact that our functions are full-fledged objects, each function in JavaScript is associated with Function.prototype , which means we can easily add methods to them.var add = function ( num1 , num2 ) {
return num1 + num2 ;
}
array = [ 3 , 4 ] ;
add. apply ( null , array ) ; // 7
this
refers to null
(the function is not an object), and the array is bound to num1
and num2
. But let's continue experimenting with the first parameter:var obj = {
data : 'Hello World'
}
var displayData = function ( ) {
alert ( this . data ) ;
}
displayData ( ) ; // undefined
displayData. apply ( obj ) ; // Hello World
apply
to bind this
to obj
. As a result, we are able to get the value of this.data
. The real value of apply lies precisely in the binding of this
.call
operator, similar to apply
all, except that it receives not a parameter, but a list of arguments.Source: https://habr.com/ru/post/155815/
All Articles