Created
November 16, 2020 09:20
-
-
Save cihat/e833d3ed62fa08be99b12d09fedd3e53 to your computer and use it in GitHub Desktop.
call() method in JavaScript
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
The call() Method | |
The first function method for manipulating this is call(), which executes | |
the function with a particular this value and with specific parameters. | |
The first parameter of call() is the value to which this should be equal | |
when the function is executed. All subsequent parameters are the parameters that should be passed into the function. For example, suppose you | |
update sayNameForAll() to take a parameter: | |
function sayNameForAll(label) { | |
console.log(label + ":" + this.name); | |
} | |
var person1 = { | |
name: "Nicholas" | |
}; | |
var person2 = { | |
name: "Greg" | |
}; | |
var name = "Michael"; | |
sayNameForAll.call(this, "global"); // outputs "global:Michael" | |
sayNameForAll.call(person1, "person1"); // outputs "person1:Nicholas" | |
sayNameForAll.call(person2, "person2"); // outputs "person2:Greg" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment