//定义一个类
var Person = function(name,age){
this.name=name;
this.age=age;
var _sex = '男';
this.getSex = function(){
return _sex;
};
this.setSex = function(sex){
this._sex=sex;
};
};
//增加属性、方法
//第一种:
/*
Person.prototype.id=10;
Person.prototype.method=function(){
alert(this.age);
};
*/
//第二种:采用
Person.prototype={
constructor:Person,
id:10,
method:function(){
alert('method...');
}
};
var p = new Person('张三',30);
alert(p.name);
alert(p.getSex());
alert(p.id);
p.method();
javascript(二) JavaScript之创建类
最新推荐文章于 2022-08-06 10:14:49 发布