return { getName: function () { return name; }, setName: function (newName) { name = newName; } }; }; var personOne = new Person(); personOne.setName("Rob"); var personTwo = new Person(); personTwo.setName("Hannah"); alert(personOne.getName()); Alert: "Rob"
return { getName: function () { return name; }, setName: function (newName) { name = newName; } }; }; var personOne = new Person(); personOne.setName("Rob"); var personTwo = new Person(); personTwo.setName("Hannah"); alert(personTwo.getName()); Alert: "Hannah"
{ name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.call(personOne, personTwo); Alert: "Hello Hannah, my name is Steve"
{ name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.call(personTwo, personOne); Alert: "Hello Steve, my name is Hannah"
{ name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.apply(personOne, [ personTwo ]); Alert: "Hello Hannah, my name is Steve"
{ name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.apply(personTwo, [ personOne ]); Alert: "Hello Steve, my name is Hannah"
Person.prototype = { sayDelayedHello: function (delay) { setTimeout(this.sayHello, delay); }, sayHello: function () { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000);
Person.prototype = { sayDelayedHello: function (delay) { setTimeout(this.sayHello, delay); }, sayHello: function () { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000); Alert: "Hello, my name is "
args = Array.prototype.concat.call(arguments); callback.apply(scope, args); }; } var Person = function() { this.name = "Steve"; }; Person.prototype = { sayDelayedHello: function(delay) { setTimeout(bind(this.sayHello, this), delay); }, sayHello: function() { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000); Alert: "Hello, my name is Steve"