this
and two arguments passed to the function. function makeArray(arg1, arg2){ return [ this, arg1, arg2 ]; }
makeArray('one', 'two'); // => [ window, 'one', 'two' ]
window
object come from? Why is this
window
equal to us?makeArray
is not just a function, “walking” by itself. In fact, makeArray
is a method of a global object (in the case of execution of code in the browser) window
. Proving it is easy: alert( typeof window.methodThatDoesntExist ); // => undefined alert( typeof window.makeArray ); // => function
makeArray('one', 'two');
equivalent to calling window.makeArray('one', 'two');
.myFunction()
), the value of this
will be a global object ( window
if the code is executed in the browser).makeArray
its method. We declare the object using literal notation, and then call our method: // var arrayMaker = { someProperty: '- ', make: makeArray }; // make() arrayMaker.make('one', 'two'); // => [ arrayMaker, 'one', 'two' ] // , arrayMaker['make']('one', 'two'); // => [ arrayMaker, 'one', 'two' ]
this
in this case is the object itself. Why not window
, as in the previous case, because the function declaration has not changed? The whole secret is how functions are passed to javascript. Function
is a standard type of JavaScript that is actually an object, and like any other object, functions can be transferred and copied. In this case, we kind of copied the entire function, including the argument list and body, and assigned the resulting object to the make
property of the arrayMaker
object. This is equivalent to this ad: var arrayMaker = { someProperty: '- '; make: function (arg1, arg2) { return [ this, arg1, arg2]; } };
obj.myFunction()
or obj['myFunction']()
, this
will have the value obj
. <input type="button" value="Button 1" id="btn1" /> <input type="button" value="Button 2" id="btn2" /> <input type="button" value="Button 3" id="btn3" onclick="buttonClicked();" /> <script type="text/javascript"> function buttonClicked(){ var text = (this === window) ? 'window' : this.id; alert( text ); } var button1 = document.getElementById('btn1'); var button2 = document.getElementById('btn2'); button1.onclick = buttonClicked; button2.onclick = function(){ buttonClicked(); }; </script>
this
inside the function will receive the value of the object to which this method belongs. Clicking on the second button will produce a “window” , because in this case we call buttonClicked
directly (that is, not as obj.buttonClicked()
). The same thing happens when we assign an event handler to an element tag, as in the case of the third button. Clicking on the third button will display the same message as the second.this
in the event handler so that the value of this
element that triggered the event: // jQuery $('#btn1').click( function() { alert( this.id ); // jQuery , 'this' });
this
? Read below.apply()
and call()
this
. If you remember, functions in JavaScript are objects. In practice, this means that functions have predefined methods. apply()
and call()
are two of them. They allow you to override the value of this
: var car = { year: 2008, model: 'Dodge Bailout' }; makeArray.apply( car, [ 'one', 'two' ] ); // => [ car, 'one', 'two' ] makeArray.call( car, 'one', 'two' ); // => [ car, 'one', 'two' ]
this
. The differences between them are in the following arguments: Function.apply()
takes an array of values ​​that will be passed to the function, and Function.call()
takes the arguments separately. In practice, in my opinion, it is more convenient to apply()
.this
without copying the function to another object, you can use myFunction.apply( obj )
or myFunction.call( obj )
.prototype
, which is a property of the constructor function. Let's create our type: // function ArrayMaker(arg1, arg2) { this.someProperty = ''; this.theArray = [ this, arg1, arg2 ]; } // ArrayMaker.prototype = { someMethod: function () { alert(' someMethod'); }, getArray: function () { return this.theArray; } }; var am = new ArrayMaker( 'one', 'two' ); var other = new ArrayMaker( 'first', 'second' ); am.getArray(); // => [ am, 'one', 'two' ]
new
before the function call. If it were not for him, it would be a global call, and the properties created in the constructor would refer to a global object. We do not need this. In addition, constructors usually do not return values ​​explicitly. Without the new
operator, the constructor would return undefined
, with it it returns this
. A good style is the name of designers with a capital letter; This will remind you of the need for the new
operator.this
in this case is a new object that you are creating.new
operator, the value of this
will be a new object created by the JavaScript runtime. If this function does not return any object explicitly, this will be implicitly returned.this
, so it makes sense to prevent them from occurring in advance.Source: https://habr.com/ru/post/119841/
All Articles