码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 继承 constructor 需要注意点

时间:2014-10-30 00:00:52      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   java   sp   div   on   

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

</body>
<script>
    var People = function(){
        this.name = "Jackey";
    };
    People.prototype.getName = function(){
        return this.name;
    };

    var Man = function(){
        this.sex = "male";
        People.call(this);
    };
    //缺点 ,name 是父类中的属性,如果修改会影响子类
    //Man.prototype = People.prototype;
    //new 新开内存
    Man.prototype = new People();
    Man.prototype.constructor = Man;//可随意修改People的属性,不会影响到Man

    var man = new Man();
    console.log(man.getName());

</script>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

</body>
<script>
    var People = function(){
        this.name = "Jackey";
    };
    People.prototype.getName = function(){
        return this.name;
    };

    var Man = function(){
        this.sex = "male";
        People.call(this);
    };
    //缺点 ,name 是父类中的属性,如果修改会影响子类
    //Man.prototype = People.prototype;
    //new 新开内存
    Man.prototype = new People();
    Man.prototype.constructor = Man;//可随意修改People的属性,不会影响到Man

    var man = new Man();
    console.log(man.getName());

</script>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

</body>
<script>
    var People = function(){
        this.name = "Jackey";
    };
    People.prototype.getName = function(){
        return this.name;
    };

    var Man = function(){
        this.sex = "male";
        People.call(this);
    };
    //缺点 ,name 是父类中的属性,如果修改会影响子类
    //Man.prototype = People.prototype;
    //new 新开内存
    Man.prototype = new People();
    Man.prototype.constructor = Man;//可随意修改People的属性,不会影响到Man

    var man = new Man();
    console.log(man.getName());

</script>
</html>

 

javascript 继承 constructor 需要注意点

标签:style   blog   io   color   ar   java   sp   div   on   

原文地址:http://www.cnblogs.com/lihaozhou/p/4060862.html

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