JavaScript Function bind()

The JavaScript Function bind() method creates a new function.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function.bind (thisArg [arguments])
function.bind (thisArg [arguments])
function.bind (thisArg [arguments])

Parameters:
thisArg: It is used as this keyword for the call to a function.
arguments: It represents the function parameters.

Return:
A new function will be the replica of the calling function.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var jewel =
{
stone: "DIAMOND",
getjewel: function()
{
return this.stone;
}
}
var getjewel1 = jewel.getjewel;
var getjewel2 = getjewel1.bind(jewel);
document.writeln(getjewel2());
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var jewel = { stone: "DIAMOND", getjewel: function() { return this.stone; } } var getjewel1 = jewel.getjewel; var getjewel2 = getjewel1.bind(jewel); document.writeln(getjewel2()); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var jewel =
{
stone: "DIAMOND",
getjewel: function()
{
return this.stone;
}
}
var getjewel1 = jewel.getjewel;
var getjewel2 = getjewel1.bind(jewel);
document.writeln(getjewel2());
</script>
</body>
</html>