首页 > 编程语言 > 详细

JavaScript原型链

时间:2021-02-10 12:52:46      阅读:29      评论:0      收藏:0      [点我收藏+]
<!doctype html>
<html lang=‘en‘>
<head>
    <title>原型</title>
    <script>
    window.onload = function(){
        //apply()  call()区别
        function Cat(name,color){   //猫对象
            this.name = name;
            this.color = color;
        };
        var cat = new Cat();
        var o = {};
        Cat.apply(o,[‘111‘,‘222‘]);  //Cat函数在O对象中执行
        o.name;//"111"
        //call
        function Cat(name,color){   //猫对象
            this.name = name;
            this.color = color;
        };
        var cat = new Cat();
        var o = {};
        Cat.call(o,‘aaa‘,‘bbb‘);  //Cat函数在O对象中执行
        o.name;//"aaa"


        function Animal(){   //动物对象   父
            this.type = ‘动物‘
        };
        Animal.prototype.type2=‘动物‘;
        function Cat(name,color){   //猫对象   子
            this.name = name;
            this.color = color;
        };
        Cat.prototype = Animal.prototype;
        var cat = new Cat("大明","黄色");   //实例化
        console.log(cat.type);


        function People() {
            this.name=‘sonia‘;
            this.sayName= function() {console.log(this.name)};
        };
        People.prototype.walk="walk";
        var p1=new People();
        p1.name="p1";
        p1.sayName= function() {console.log(‘p1‘)};
        var p2=new People();
        p2.name="p1";
        p2.sayName= function() {console.log(‘p2‘)};


        //原型链
        function F1(){
            this.name1=‘1‘
        };
        function F2(){
            this.name2=‘2‘
        };
        function F3(){
            this.name3=‘3‘
        };
        F2.prototype = new F1();
        F3.prototype = new F2();
        var ff = new F3();
        console.log(ff.name2);
        //修改原型数据
        function F1(){
            //this.name1=‘1‘
        };
        F1.prototype.name1 = ‘f1‘;
        function F2(){
            this.name2=‘2‘
        };
        function F3(){
            this.name3=‘3‘
        };
        F2.prototype = new F1();
        F3.prototype = new F2();
        var ff = new F3();
        console.log(ff.__proto__.__proto__.__proto__.name1 = ‘abc‘);
	    console.log(F1.prototype);

        //删除原型数据
        function F1(){
            //this.name1=‘1‘
        };
        F1.prototype.name1 = ‘f1‘;
        function F2(){
            this.name2=‘2‘
        };
        function F3(){
            this.name3=‘3‘
        };
        F2.prototype = new F1();
        F3.prototype = new F2();
        var ff = new F3();
        delete ff.__proto__.__proto__.__proto__.name1;
	    console.log(F1.prototype);

        //原型链    构造函数 /原型/实例 的关系
        //函数对象中有prototype   原型对象prototype里有constructor属性,指向原型对象所属的构造函数
        //对象都有__proto__属性  指向函数的原型对象
    }
    </script>
</head>
<body>
    
</body>
</html>

JavaScript原型链

原文:https://www.cnblogs.com/Dewumu/p/14395357.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!