Created
May 9, 2013 01:53
-
-
Save PabloVallejo/5544996 to your computer and use it in GitHub Desktop.
Sample of call and apply methods
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
// Person | |
var Person = function( firstName, lastName ) { | |
// Asign attributes | |
this.first_name = firstName; | |
this.last_name = lastName; | |
// Say name method | |
this.sayName = function() { | |
console.log( "%s %s", this.firstName, this.lastName ); | |
} | |
}; | |
// Person 1 | |
var jane = new Person( "Jane", "Smith" ); | |
jane.sayName(); // Jane Smith | |
// Person 2 | |
var peter = new Person( "Peter", "Doe" ); | |
petter.sayName(); // Petter Doe | |
// Using call to invoke method with the passed context | |
jane.sayName.call( petter ); // "Petter Doe" | |
// now on person 2 | |
petter.sayName.call( jane ); // Jane Doe | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment