JavaScript原型链

<!doctype html>
<html lang="en">
<head>
    <title>&#x539F;&#x578B;</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>

Original: https://www.cnblogs.com/Dewumu/p/14395357.html
Author: 德乌姆列特
Title: JavaScript原型链

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606634/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球