基本方法很简单,但在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)}
}
本文介绍了使用JavaScript创建类的五种不同方法,包括构造函数、原型链、闭包等,每种方法都有其特点及应用场景。
549

被折叠的 条评论
为什么被折叠?



