/*
* JavaScript模拟实体类的实现
*/
function User(){
this.name = "";
this.age = 0;
//如果传入实参直接 初始化
if (arguments.length == 2) {
this.name = arguments[0];
this.age = arguments[1];
}
this.getName = function(){
return this.name;
}
this.setName = function(name){
this.name = name;
}
this.getAge = function(){
return this.age;
}
this.setAge = function(age){
this.age = age;
}
}
var user = new User("zdw", 22);
alert("name:" + user.getName() + ",age : " + user.getAge());
user.setName("admin");
user.setAge(99);
alert("changed********name:" + user.getName() + ",age : " + user.getAge());
本文介绍了一种使用JavaScript来模拟实体类的方法。通过构造函数User初始化对象,并提供了getName、getAge等获取属性的方法以及setName、setAge等设置属性的方法。实例演示了如何创建对象并更改其属性。
757

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



