Javascript如何实现继承

2018-03-06  本文已影响0人  飞飞廉
        function Person(name){
            this.name=name;
        }
        function Child(){
            Person.call(this,'wang')
            this.age=16
        }
        var man=new Child();
        console.log(man.name,man.age)
        function Person(name){
            this.name=name;
        }
        Person.prototype.sayName=function(){
            alert(this.name)
        }
        function Child(name,age){
            Person.call(this,name);
            this.age=age;
        }
        Child.prototype=new Person();
        Child.prototype.constructor=CHild;
        var instance=new Child('lisi',18)
上一篇下一篇

猜你喜欢

热点阅读