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

原型、函数伪装(apply,call)、继承

时间:2015-12-26 16:58:24      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:js 继承



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>原型与继承</title>
    <script type="text/javascript">
        /*
         * 组合的方式是属性通过伪造的方式实现,方法通过原型链的方式实现,注意内存模型
         *
         *
         * */
        function Parent(name) {
            this.name = name;
            this.color = ["red", "blue"];

        }

        Parent.prototype.ps = function () {

            alert(this.name + "[" + this.color + "]");
        }

        function Child(name, age) {
            this.age = age;
            Parent.call(this, name);


        }

        Child.prototype = new Parent();
        Child.prototype.say = function () {
            alert(this.name + "," + this.age + "[" + this.color + "]");
        }

        var c1 = new Child("Leon", 22);
        c1.color.push("green");
        c1.say();
        c1.ps();
        var c2 = new Child("Ada", 23);
        c2.say();
    </script>
</head>
<body>

</body>
</html>


通过原型的方式创建对象:

/*
*使用基于原型的方式创建可以将属性和方法设置为Person专有的,不能通过windows来调用
*
* */

function Person(){

}

Person.prototype.name="Leon";
Person.prototype.age=23;
Person.prototype.say=function(){

   alert(this.name+","+this.age);
}
var p1=new Person();
p1.say();
say();
//以下方法可以检测出p1 是否有_prop_隐藏属性指向Person的原型
alert(Person.prototype.isPrototypeOf(p1));


技术分享

参考视频:原型内存模型

http://tianxingzhe.blog.51cto.com/3390077/1728556


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1728556

原型、函数伪装(apply,call)、继承

标签:js 继承

原文地址:http://tianxingzhe.blog.51cto.com/3390077/1728556

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