Created
December 26, 2017 05:46
-
-
Save PavanKu/44a8b47fc2cdaf00093e1137b02c6721 to your computer and use it in GitHub Desktop.
this with bind method
This file contains hidden or 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
function Person(fn, ln) { | |
this.first_name = fn; | |
this.last_name = ln; | |
this.displayName = function() { | |
console.log(`Name: ${this.first_name} ${this.last_name}`); | |
} | |
} | |
let person = new Person("John", "Reed"); | |
person.displayName(); // Prints Name: John Reed | |
let person2 = new Person("Paul", "Adams"); | |
person2.displayName(); // Prints Name: Paul Adams | |
let person2Display = person.displayName.bind(person2); // Creates new function with value of “this” equals to person2 object | |
person2Display(); // Prints Name: Paul Adams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment