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

javascript继承对象的方式

2010-05-05 15:30 465 查看
//        function Person(){
//            this.name="121212";
//            this.SayHellow=function(){
//                alert(this.name+"---你好")
//            }
//        }
//
//        Person.prototype.SayGoodbye=function(){
//            alert("GoodBye!");
//        }
//
//        function man(){}
//        man.prototype=new Person();
//
//        var Hman=new man();
//        Hman.name="";
//        Hman.SayHellow();

function Person(name){//基类
this.name = name;
}
Person.prototype.sayHello = function(){alert(this.name+"say Hello!");};//基类的方法
function Student(name,id){//子类一个实例
Person.call(this,name);//在当前对象里执行基类      //只能继承构造函数(对象{}以内)以内
this.id = id;
}
Student.prototype = new Person();                          //继承构造函数(对象{}以内)以外
Student.prototype.show = function(){
alert("Name is:"+ this.name+" and Id is:"+this.id);
}
var s=new Student("11",1);
s.sayHello()//由 Student.prototype = new Person(); 继承得来
s.show()//中的this.name是由  Person.call(this,name);继承来的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: