Skip to content

Instantly share code, notes, and snippets.

@rahgurung
Last active December 2, 2021 12:11
Show Gist options
  • Save rahgurung/10929ce4c4b623e50394a2bc85a84ea5 to your computer and use it in GitHub Desktop.
Save rahgurung/10929ce4c4b623e50394a2bc85a84ea5 to your computer and use it in GitHub Desktop.
function doSomething() {
// "use strict"; // uncomment to run in strict mode
console.log( this.x );
}
var obj = {
x: "Hello World!"
}
var x = "global"
// passing the object to be used for `this` explicitly
doSomething.call(obj); // "Hello World!
doSomething.apply(obj); // "Hello World!
// passing the `null` and `undefined`, `this` will be bind to global scope
doSomething.call(null); // "Hello World!
doSomething.apply(null); // "Hello World!
doSomething.call(undefined); // "Hello World!
doSomething.apply(undefined); // "Hello World!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment