您的位置:首页 > Web前端 > JavaScript

javascript继承---原型继承的例子

2011-07-27 16:08 501 查看
<html>
<head></head>
<body>
<script type="text/javascript">
function Person(name){
this.name=name;
}

Person.prototype.getName= function(){
return this.name;
}

function User( name , passward){
this.passward= passward;
this.name=name;
}

/**
* 原型继承
* 每次调用new User()创建的对象就会自动拥有Person对象的所有方法
* */
User.prototype= new Person();

User.prototype.getPassward= function(){
return this.passward;
}

var per= new Person("Amy");
document.writeln(per.name);
document.writeln(per.getName());

var use=new User("hello",18);
document.writeln(use.name);
document.writeln(use.passward);
document.writeln(use.getName());
document.writeln(use.getPassward());

</script>
</body>
</html>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: