<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function Emp(x,y,z){
this.empno =x;
this.ename =y;
this.sal =z;
this.work =function(){
conction.log(this.ename+"is working")
}
}
//创建对象
var a = new Emp();
var b = new Emp(1);
var c = new Emp(1,"KING");
var d = new Emp(2,"yyy",777);
//c.work()
//动态扩展方法
Emp.prototype.getsal = function(){
return this.sal;
}
//调用
c.getsal();
</script>
</body>
</html>
在JS中定义类的方式有两种
第一种
function 类名(形式参数列表){
this.属性名=参数;
this.方法名=function(){
}
}
第二种
类名= function(形式参数列表){
this.属性名=参数;
this.方法名=function(形式参数列表){
}
}