Last active
July 9, 2019 11:50
-
-
Save cyrus-and/8169187 to your computer and use it in GitHub Desktop.
JavaScript `this` examples from http://stackoverflow.com/a/12371105/477168
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript `this` examples from http://stackoverflow.com/a/12371105/477168 | |
// The value of this in a function is sometimes called the "context" in which | |
// the function runs. The most important thing to understand is that a function | |
// object does not have a fixed context -- the value of this changes depending | |
// on how the function is called. | |
// If the function is called as a "raw" function (e.g., just do someFunc()), | |
// this will be window (or undefined if the function runs in strict mode). | |
(function () { | |
console.log('this === global: ' + (this === global)); | |
})(); | |
// If it is called as a method on an object, this will be the calling object. | |
(function () { | |
var obj = { | |
'foo': function () { | |
console.log('this === obj: ' + (this === obj)); | |
} | |
}; | |
obj.foo(); | |
})(); | |
// If you call a function with call or apply, this is specified as the first | |
// argument to call or apply. | |
(function () { | |
var self = {}; | |
var foo = function () { | |
console.log('this === self: ' + (this === self)); | |
} | |
foo.call(self); | |
foo.apply(self); | |
})(); | |
// If it is called as an event listener (as it is here), this will be the | |
// element that is the target of the event. | |
(function () { | |
var events = require('events'); | |
var foo = new events.EventEmitter(); | |
foo.on('bar', function () { | |
console.log('this === foo: ' + (this === foo)); | |
}); | |
foo.emit('bar'); | |
})(); | |
// If it is called as a constructor with new, this will be a newly-created | |
// object whose prototype is set to the prototype property of the constructor | |
// function. | |
(function () { | |
var that; | |
var foo = function () { | |
that = this; | |
}; | |
var bar = new foo(); | |
console.log('this === bar: ' + (that === bar)); | |
})(); | |
// If the function is the result of a bind operation, the function will always | |
// and forever have this set to the first argument of the bind call that | |
// produced it. (This is the single exception to the "functions don't have a | |
// fixed context" rule -- functions produced by bind actually do have an | |
// immutable this). | |
(function () { | |
var self = {}; | |
var foo = function () { | |
console.log('this === self: ' + (this === self)); | |
}; | |
foo.bind(self)(); | |
})(); |
Thanks, I forgot to actually call the result of foo.bind(self)
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// suggest to update the last code example as follows:
// If the function is the result of a bind operation, the function will always
// and forever have this set to the first argument of the bind call that
// produced it. (This is the single exception to the "functions don't have a
// fixed context" rule -- functions produced by bind actually do have an
// immutable this).
// 1. bind() will create a new function
// 2. the keyword this of the new function is set to the first argument of bind() when calls
// 3. the other arguments of bind() will be given as parameters of new function when it calls
// code output:
// this === obj: true