Created
December 26, 2017 05:44
-
-
Save PavanKu/dbaa39b079d7aa1a5c7ed0f15eb5f292 to your computer and use it in GitHub Desktop.
this with Call and Apply
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 | |
person.displayName.call(person2); // Here we are setting value of this to be person2 object | |
//Prints Name: Paul Adams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment