huanongying 2013-08-18
function SimpleDemo(param1, param2){
this.param1 = param1;
this.param2 = param2;
}这是JavaScript中类的定义,与普通function不同的是,"类"以大写开头,实则是一个构造函数。var demo = new SimpleDemo('p1', 'p2');
alert( '类的属性:1.' + demo.param1 + ',2.' + demo.param2 );demo.toString = function(){
return 'param1:' + this.param1 + ',param2:' + this.param2;
}缺点:只有在该对象中使用,对于其他通过new SimpleDemo()来创建的对象均不能使用该方法。function SimpleDemo(param1, param2){
this.param1 = this.param1;
this.param2 = this.param2;
this.toString = function(){ //注意定义的方法有返回值的话,toString后不能加"()",加了就不能有返回值。
return '参数1:' + this.param1 + ',参数2:' + this.param2;
}
}可直接调用:alert(new SimpleDemo('apple', 'orange').toString());function SimpleDemo(param1, param2){
this.param1 = param1;
this.param2 = param2;
}
SimpleDemo.prototype.toString = function(){
return '参数1:' + this.param1 + ', 参数2:' + this.param2;
}
window.onload = function(){
alert(new SimpleDemo('apple', 'orange').toString());
} 或同时定义多个方法:SimpleDemo.prototype = {
toString : function(){
return '参数1:' + this.param1 + ',参数2:' + this.param2;
},
sayHello : function(){
return 'Hello World!!';
}
} var sDemo = function(){};
sDemo.name = 'static class';
sDemo.discription = 'This is a static class demo';
sDemo.distance = 0;
sDemo.forward = function(){
this.distance++;
}
sDemo.forward = function(){
this.distance--;
}
window.onload = function(){
alert(sDemo.distance); //结果为["0"]
sDemo.forward();
sDemo.forward();
sDemo.backward();
alert(sDemo.distance); //结果为["2"]
} 因为是静态类,所以没有必要使用prototype对象。用了也会报错。function People(){
this.type = 'human';
}
People.prototype = {
getType : function(){
return 'this is a mankind';
},
getActions : function(){
return 'talk/walk/hear/smell.etc';
}
}
function Student(name, sex, age){
People.apply(this, arguments);
for(var index in People.prototype){ //JavaScript中的for循环使用用来遍历数组,此处为for/in循环,用来遍历对象的属性成员
var proto = this.constructor.prototype;
if(!proto[index]){ //当前类的prototype继承父类的prototype
proto[index] = People.prototype[index];
}
proto[index]['super'] = People.prototype; //???
}
this.name = name;
this.sex = sex;
this.age = age;
}
window.onload = function(){
var s1 = new Student('Hungry', 'male', 14);
alert(s1.getType()); //结果为["this is a mankind"]
alert(s1.getActions());
} 参考资料:
1.HTML5 Canvas 游戏开发实战, by 张路斌, 机械出版社, 2013年4月
2.JavaScript:The Definitive Guide, by David Flanagan(O'Reilly).