[b]What does the new operator do?[/b]
[b]We have a 'this' context that is a Ninja object.[/b]
[b]QUIZ: Add a method that gives a name to the ninja.[/b]
[b]Add a new property and method to the object.[/b]
[b]What happens when we forget to use the new operator?[/b]
[b]What happens when we forget to use the new operator? (cont.)[/b]
[b]We need to make sure that the new operator is always used.[/b]
[b]QUIZ: Is there another, more generic, way of doing this?[/b]
[b]A solution using arguments.callee.[/b]
[b]Flexib
function Ninja(){
this.name = "Ninja";
}
var ninjaA = Ninja();
assert( !ninjaA, "Is undefined, not an instance of Ninja." );
var ninjaB = new Ninja();
assert( ninjaB.name == "Ninja", "Property exists on the ninja instance." );
[b]We have a 'this' context that is a Ninja object.[/b]
function Ninja(){
this.swung = false;
// Should return true
this.swingSword = function(){
this.swung = !this.swung;
return this.swung;
};
}
var ninja = new Ninja();
assert( ninja.swingSword(), "Calling the instance method." );
assert( ninja.swung, "The ninja has swung the sword." );
var ninjaB = new Ninja();
assert( !ninjaB.swung, "Make sure that the ninja has not swung his sword." );
[b]QUIZ: Add a method that gives a name to the ninja.[/b]
function Ninja(name){
// Implement!
}
var ninja = new Ninja("John");
assert( ninja.name == "John", "The name has been set on initialization" );
ninja.changeName("Bob");
assert( ninja.name == "Bob", "The name was successfully changed." );
[b]Add a new property and method to the object.[/b]
function Ninja(name){
this.changeName = function(name){
this.name = name;
};
this.changeName( name );
}
var ninja = new Ninja("John");
assert( ninja.name == "John", "The name has been set on initialization" );
ninja.changeName("Bob");
assert( ninja.name == "Bob", "The name was successfully changed." );
[b]What happens when we forget to use the new operator?[/b]
function User(first, last){
this.name = first + " " + last;
}
var user = User("John", "Resig");
assert( typeof user == "undefined", "Since new wasn't used, the instance is undefined." );
[b]What happens when we forget to use the new operator? (cont.)[/b]
function User(first, last){
this.name = first + " " + last;
}
window.name = "Resig";
var user = User("John", name);
assert( name == "John Resig", "The name variable is accidentally overridden." );
[b]We need to make sure that the new operator is always used.[/b]
function User(first, last){
if ( !(this instanceof User) )
return new User(first, last);
this.name = first + " " + last;
}
var name = "Resig";
var user = User("John", name);
assert( user, "This was defined correctly, even if it was by mistake." );
assert( name == "Resig", "The right name was maintained." );
[b]QUIZ: Is there another, more generic, way of doing this?[/b]
function User(first, last){
if ( !(this instanceof ___) )
return new User(first, last);
this.name = first + " " + last;
}
var name = "Resig";
var user = User("John", name);
assert( user, "This was defined correctly, even if it was by mistake." );
assert( name == "Resig", "The right name was maintained." );
[b]A solution using arguments.callee.[/b]
function User(first, last){
if ( !(this instanceof arguments.callee) )
return new User(first, last);
this.name = first + " " + last;
}
var name = "Resig";
var user = User("John", name);
assert( user, "This was defined correctly, even if it was by mistake." );
assert( name == "Resig", "The right name was maintained." );
[b]Flexib