javascript 继承

来源:互联网 发布:苏小红c语言第三版pdf 编辑:程序博客网 时间:2024/06/07 03:28

高级语法的基本使用

<script type="text/javascript">    //声明一个函数demo        function Demo()    {    }    //实例函数demo    var demo = new Demo();    //声明一个函数Demo1    function Demo1(name,age)    {        this.name = name;        this.age = age;    }    //实例一个函数Demo1    var demo1 = new Demo1('谭勇',21);    //运行下面试试    console.log(demo1.name);    console.log(demo1.age);    //Demo2    function Demo2(name,age)    {        var that = this;        this.name = name;        this.age = age;        function a()        {            return that.name;        }        function b()        {            return that.age;        }        this.getName = a;        this.getAge  = b;    }    //实例一个函数Demo1    var demo2 = new Demo2('谭勇',21);    //运行下面试试    console.log(demo2.name);    console.log(demo2.age);</script>

继承

<script type="text/javascript">    function Demo(name,age)    {        this.name = name;        this.age = age;    }    function  Son()    {        this.text = 'test text';    }    Son.prototype = new Demo('谭勇',22);    var __son = new Son();    //试试    console.log(__son.name);    console.log(__son.age);    console.log(__son.text);    //组合继承    function Son1()    {        this.text1 = 'my test son1';    }    Son1.prototype.Demo = new Demo('谭勇',22);    Son1.prototype.Son1 = new Son1();    var __son1 = new Son1();    console.log(__son1.Demo.name);    console.log(__son1.Demo.age);    console.log(__son1.Son1.text);    console.log(__son1.text1);</script>
原创粉丝点击