<!doctype>
<html>
<head>
<meta charset='utf-8' />
<title>js obj test</title>
<script>
//创建类
function class_user(){
this.name = null;
this.sex = null;
this.age = null;
}
//类相关函数 通过 prototype 添加
class_user.prototype.set_name = function(name){
this.name = name;
}
class_user.prototype.get_name = function(){
this.use_class_method();//使用类中的函数
return this.name;
}
class_user.prototype.use_class_method = function(){
alert('使用类中的函数!');
}
//测试
window.onload = function(){
var user = new class_user();
user.name = 'old_name';
user.sex = 'old_sex';
user.age = 23;
alert(user.name + ' ' + user.sex + ' ' + user.age);
user.set_name('new_name');
alert(user.get_name());
}
</script>
</head>
<body>
</body>
</html>
js面向对象 - mark
最新推荐文章于 2024-05-12 09:39:11 发布