日期:2014-05-16  浏览次数:20355 次

javascript类的创建的多种方式

基本方法很简单,但在javascript中创建类有多种方式。

看例子代码,主要是类方法的写法不一样。

1.?function people(name){
???????? ?this.name=name;
??????????this.printInfo=function(){document.write(this.name);};
??}
??

2.?

? ?var people=function(name){
???this.name=name;
???this.printInfo=function(){document.write(this.name)}
??}

?

3.

??var people=function(name){
???this.name=name;
???this.printInfo=printInfo;
??}
??
??function printInfo(){document.write(this.name)}

?

4.??var people=function(name){
???this.name=name;
??}
??
??people.prototype.printInfo=function(){document.write(this.name)}

?

5.??var people=function(name){
???this.name=name;
??}
??
??people.prototype={
???printInfo:function(){document.write(this.name)}
??}

?

?