81,094
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
// 声明一个 Student 的类,同时有个构造方法
// 当然,也可以写成这样:
/*
function Student(name) {
this.name = name;
}
*/
var Student = function(name) {
this.name = name;
};
// 给 Student 上添加一个 getName 函数
// 注意这个与这个的区别:
/*
// 这种相当于 Java 中的静态方法
Student.getName = function() {
return 'name';
}
*/
// 这种相当于 Java 中的类方法
Student.prototype.getName = function() {
return this.name;
};
Student.prototype.setName = function(name) {
this.name = name;
};
Student.prototype.toString = function() {
return 'name: ' + this.name;
};
window.onload = function() {
var student = new Student('tom');
alert(student.getName());
alert(student.toString());
}
</script>
<script type="text/javascript">
var Student = function(name) {
this.name = name;
};
Student.prototype.getName = function() {
return this.name;
};
Student.prototype.setName = function(name) {
this.name = name;
};
Student.prototype.toString = function() {
return 'name: ' + this.name;
};
window.onload = function() {
var student = new Student('tom');
alert(student.getName());
alert(student.toString());
}
</script>
<script type="text/javascript">
var Student = function(name) {
this.name = name;
}
Student.prototype = {
getName : function() {
return this.name;
},
setName : function(name) {
this.name = name;
},
toString : function() {
return 'name: ' + this.name;
}
}
window.onload = function() {
var student = new Student('tom');
alert(student.getName());
alert(student.toString());
}
</script>