<html>
<script>
/*继承
function Person(name,age){
this.name=name;
this.age=age;
this.say=function(){
alert('say....');
}
}
function Boy(){
}
Boy.prototype=new Person('z3',20);
var b=new Boy();
alert(b.name);
alert(b.age);
b.say();
function Person(name,age){
this.name=name;
this.age=age;
this.say=function (){
alert('say.....');
}
}
function Boy(){
}
Boy.prototype=new Person('z3',20);
var b=new Boy();
alert(b.name);
alert(b.age);
b.say();
*/
//prototype继承和扩展
function Person(name,age){
this.name=name;
this.age=age;
this.say=function(){
alert('say....');
}
Person.prototype.id=1;
Person.prototype.method=function(){
alert('ext method');
}
function Boy(name,age){
}
Boy.prototype=new Person('w5',20);
var b=new Boy();
alert(b.name);
alert(b.age);
b.say();
alert(b.id);
b.method();
}
</script>
</html>
js中prototype的继承和扩展
最新推荐文章于 2022-03-03 17:16:15 发布