js面向对象编程,原型继承1
var dice={
sides:6,
roll:function(){
var randomNumber=Math.floor(Math.random()*this.sides)+1;//this的用法
return randomNumber;
}
}
var button=document.getElementById("button");
button.onclick=function(){
var result=dice.roll();
printNumber(result);
}
function printNumber(number){
var placeholder=document.getElementById("placeholder");
placeholder.innerHTML=number;
}
var calculator={ sum:0, add:function(value){ this.sum=this.sum+value;//this取得本函数外部sum }, clear:function(){ this.sum=0; }, equals:function(){ return this.sum; } }
function Contact(name,email){ this.name=name;//参数赋值给this.name this.email=email; } var contact=new Contact("Andr","ad@dd.com");