Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions JavaScript_Advance/bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*The bind() method creates a new function that, when called, has its this keyword set
to the provided value, with a given sequence of arguments preceding any provided
when the new function is called.
*/
let module = {
x: 42,
getX: function() {
return this.x;
}
}

let unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined

let boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42

21 changes: 21 additions & 0 deletions docs/JavaScript_Advance/bind.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

*The **bind()** method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.*

let module = {
x: 42,
getX: function() {
return this.x;
}
}

let unboundGetX = module.getX;

console.log(unboundGetX());

> The above function gets invoked at the global scope
> expected output: undefined

let boundGetX = unboundGetX.bind(module);
console.log(boundGetX());

> expected output: 42