原型

101 阅读1分钟

实例与原型链


        function Person(){
            
        }

        Person.prototype.name = "kaola";

        let person = new Person();

        person.name = "person";

        console.log(person.name);   //person

        delete person.name;

        console.log(person.name);   //kaola
        
        console.log(Object.prototype.__proto__);    //null
        
        
        
        
        

然而 null 究竟代表了什么呢?

引用《undefined与null的区别》 就是: null 表示“没有对象”,即该处不应该有值。

顺便还要说一下,图中由相互关联的原型组成的链状结构就是原型链,也就是蓝色的这条线。

        var scope = "global scope";

        function checkscope(){
            var scope = "local scope";
            function f(){
                return scope;
            }
            return f();
        }

        checkscope();
        
        var scope = "global scope";
        function checkscope(){
            var scope = "local scope";
            function f(){
                return scope;
            }
            return f;
        }
        checkscope()();


        var data = [];


        for (var i = 0; i < 3; i++) {
            data[i] = function () {
                console.log(i);
            };
        }

        data[0]();
        data[1]();
        data[2]();

        // let data = [];

        // for (let i = 0; i < 3; i++) {
        //     data[i] = (function (i) {
        //             return function(){
        //                 console.log(i);
        //             }
        //     })(i);
        // }

        // data[0]();
        // data[1]();
        // data[2]();
        
        console.log(this instanceof Object);

        // 都能生效
        console.log(Math.random());
        console.log(this.Math.random());

        a = 1;
        console.log(a);