this, written to explain the true problem

  • Precompiled function during this - "window
  • This global scope - "window
  • call / apply this to a function can be changed at runtime
  • Who call the function, who is this
    obj.function () {} function () inside this point obj
function test(c) {
            // var this = Object.create(test.prototype); {
            //     __proto__: test.prototype;
            // }
            var a = 123;

            function b() {
                //sjflkjasdlj
            }
        }
        // ao{
        //     a : undefined
        //     b : function(){}
        //     this : window
        //     arguement : [1]
        // }
        test(1);
var name = "222";
        var a = {
            name: "111",
            say: function() {
                console.log(this.name);
            }
        };
        var fun = a.say;
        fun();
        a.say();
        var b = {
            name: "333",
            say: function(fun) {
                fun();
            }
        };
        b.say(a.say);
        b.say = a.say;
        b.say();

Here Insert Picture Description
Difficult to understand is the third; execution b.say () method, this means that b, but a.say parameter, he is a function, the function execution empty, no one to call! Belongs to the pre-compiled, this value windows so nothing to do with a, b!

arguments

  • arguments.callee
  • function.caller

callee quote refers to the function! It refers to the function itself, the initialization tool - immediate execution of the function

var jiecheng = (function(i) {
            if (i == 1) return i;
            return i * jiecheng(i - 1);
        })(5);

Here, the factorial function is immediately execute the function, which want to achieve recursion, you must call the function itself! So we used the arguments.callee!

var jiecheng = (function(i) {
            if (i == 1) return i;
            return i * arguments.callee(i - 1);
        })(5);

Here Insert Picture Description
The function itself refers to the property demo.caller environment which calls!

function test() {
            demo();
        }

        function demo() {
            console.log(demo.caller);
        }
        test();

In es5 not use the callee and caller!

Do question it!

var foo = "123";

        function print() {
            var foo = "456";
            this.foo = "789";
            console.log(foo);
        }
        print();

Answer: 456

var foo = 123;

        function print() {
            this.foo = 234;
            console.log(foo);
        }
        print();

Answer: 234

var foo = 123;

        function print() {
            this.foo = 234;
            console.log(foo);
        }
        new print();

Using the new method, that is, print a constructor, which is the object of this refers to his prototype creation, to be printed is foo, not this.foo, so on the go who find --123

var a = 5;

        function test() {
            a = 0;
            alert(a);
            alert(this.a);
            var a;
            alert(a);
        }
        test();
        // new test();

0 5 0
0 undefined 0

function print() {
            console.log(foo);
            var foo = 2;
            console.log(foo);
            console.log(hello);
        }
        print();

Here Insert Picture Description

function print() {
            var test;
            test();

            function test() {
                console.log(1);
            }
        }
        print();

Answer: 1

 function print() {
            var x = 1;
            if (x == "1") console.log("one!");
            if (x === "1") console.log("two!");
        }
        print();

The answer: one!

var bar = {
            a: "002"
        };

        function print() {
            bar.a = "a";
            Object.prototype.b = "b";
            return function inner() {
                console.log(bar.a);
                console.log(bar.b);
            };
        }
        print()();

Here Insert Picture Description

var obj = {
            naem: "abc",
            age: 123,
            sex: "famale"
        };
        var obj1 = {};

        function clone(origin, target) {
            for (var prop in origin) {
                target[prop] = origin[prop];
            }
        }
        clone(obj, obj1);

Here Insert Picture Description

Published 37 original articles · won praise 0 · Views 699

Guess you like

Origin blog.csdn.net/weixin_43704007/article/details/105023349