码迷,mamicode.com
首页 > Web开发 > 详细

js 继承

时间:2015-06-10 18:48:21      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>

    <body>
        <script>
            /**
             * 第一种继承
             */
            function Persion(name) {
                this.name = name ? name : ‘persion‘;
            };

            Persion.prototype.run = function() {
                console.log(this.name)
            };

            function Xiaoming(name) {
                Persion.call(this, name);
            };

            Xiaoming.prototype.run = function() {
                console.log(this.name)
            };

            var xm = new Xiaoming(‘xiaoming‘);
            // xm.run();
            // console.log(xm.constructor)

            /**
             * 第二种继承
             */
            function Persion2(name) {
                this.name = name ? name : ‘persion2‘;
            };

            Persion2.prototype.run = function() {
                console.log(this.name)
            };

            function Xiaoming2(name) {
                Persion2.call(this, name);
            };

            Xiaoming2.prototype = new Persion2();

            var xm2 = new Xiaoming2(‘xiaoming2‘);
            // xm2.run();
            // console.log(xm2.constructor)

            /**
             * 第三种继承--比较全面
             */
            function Persion3(name) {
                this.name = name ? name : ‘persion3‘;
            };

            Persion3.prototype.run = function() {
                console.log(this.name)
            };

            function Xiaoming3(name) {
                //步骤 1
                Persion3.call(this, name);
            };

            //步骤 2--这时候,Xiaoming3的constructor指向Persion3,所以,需要步骤3来重置它
            Xiaoming3.prototype = new Persion3();
            //步骤 3
            Xiaoming3.prototype.constructor = Xiaoming3;

            var xm3 = new Xiaoming3(‘xiaoming3‘);
            // xm3.run();
            // console.log(xm3.constructor)

        </script>
    </body>

</html>

  

js 继承

标签:

原文地址:http://www.cnblogs.com/simonbaker/p/4566877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!