Instantiation

[b]What does the new operator do?[/b]
 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值