javascript类的创建方式
定义javascript类,在javascript中没有java语言class关键字,但有function。
类的使用1.定义,2.使用
类同样有属性和方法
1.定义
语法
var 类名=function(参数列表1,参数列表2){
this.变量=参数列表1;
this.变量=参数列表2;
}
例:定义Person类
function people(name){ this.name=name; this.printInfo=function(){document.write(this.name);}; }
2.使用类
var 对象名=new 构造函数();
对上面使用
var p = new people("张三"); p.printInfo();
完整代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>javascript类创建</title>
<script type="text/javascript">
function people(name){
this.name=name;
this.printInfo=function(){document.write(this.name);};
}
function p1(){
var p = new people("张三");
p.printInfo();
}
</script>
</head>
<body>
<a href="javascript:void 0;" οnclick="p1()">类创建</a>
</body>
</html>