<script>
/**
* 以下方式将会重写原型
* 由于原型重写,而且没有通过Person.prototype来指定
* 此时的constructor不会再指向Person而是指向Object
* 如果constructor真的比较重要,可以在json中说明原型的指向
*/
function Person() {};
Person.prototype = {
name:"Jack",
age:25,
say:function() {
alert(this.name + ", " + this.age);
}
};
var p1 = new Person();
p1.say();
alert(p1.constructor);
alert(p1.constructor == Person);
function Student() {};
Student.prototype = {
constructor:Student, // 手动指定constructor
name:"Ann",
age:21,
say:function() {
alert(this.name + ", " + this.age);
}
};
var s1 = new Student();
s1.say();
alert(s1.constructor == Student);
</script>
JavaScript中原型的重写
最新推荐文章于 2024-08-06 14:56:17 发布
