Skip to content

Instantly share code, notes, and snippets.

@PabloVallejo
Created May 9, 2013 01:53
Show Gist options
  • Save PabloVallejo/5544996 to your computer and use it in GitHub Desktop.
Save PabloVallejo/5544996 to your computer and use it in GitHub Desktop.
Sample of call and apply methods
// 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