Node.js this指標指向module.exports、global、例項,指標顯式、隱式傳遞與繫結與優先順序

陳楊發表於2019-02-16

一、this指標指向module.exports

console.log("全域性中的this指向的是module.exports");
console.log(this); //{}
this.obj = "Hello World";
console.log(this.obj); //Hello World
console.log(global.obj); //undefined
console.log(module.exports.obj); //Hello World
console.log("-------------------------------------------------------" + "

");

二、this指標指向global物件

console.log("在函式中this指向的是global物件,和全域性中的this不是同一個物件");
function fn() {
    this.obj = "good good study! day day up!";
}

fn();
console.log(this);//{ obj: `Hello World` }
console.log(this.obj);//Hello World
console.log(global.obj);//"good good study! day day up!"

console.log("-------------------------------------------------------" + "

");
console.log("在函式中this指向的是global物件,和全域性中的this不是同一個物件");
function fn1() {
    function fn2() {
        this.msg = "I love you";
    }

fn2();
console.log(this); //global
console.log(this.msg); //"I love you"
console.log(global.msg); //"I love you"

}

fn1();
console.log("-------------------------------------------------------" + "

");

三、在建構函式中this指向的是它的例項,而不是global

function Fn3(){
    this.year = 1998;
}
let fn3 = new Fn3();
console.log(this); //{ obj: `Hello World` }
console.log(fn3.year); //1998
console.log(global.year); //undefined

console.log("-------------------------------------------------------" + "

");

四、this指標顯式、隱式傳遞與繫結

console.log("顯式傳遞this");
let Kirito = {};

function person(name, sex, age, addr, salary) {
    this.name = name;
    this.sex = sex;
    this.age = age;
    this.addr = addr;
    this.salary = salary;
}

//這裡的傳入Kirito為this指標所指向的物件
//使用.call()進行顯式傳遞
person.call(Kirito, "桐人",
    "男",
    18,
    "SAO",
    999999999);
console.log(Kirito);

console.log("-------------------------------------------------------" + "

");
console.log("隱式傳遞this");
let Ausua = {
    name: "亞絲娜",
    sex: "女",
    age: 18,
    addr: "SAO",
    salary: 999999999,
    func() {
        console.log(this);
    },
    func_bind: function () {
        console.log(this);
    }.bind("繫結")

};
Ausua.func();

console.log("-------------------------------------------------------" + "

");
console.log("強制繫結this指標");
let func = function () {
    console.log(this);
}.bind(Kirito);

func();
console.log("-------------------------------------------------------" + "

");
console.log("注意:
	這裡的func是在原來的物件基礎上,使用bind繫結了this指標,產生了新的函式物件!");
func = function () {
    console.log(this);
};

//注意:func此時繫結物件後,func變數的物件引用指標 指向繫結前
func.bind(Kirito);
func();

//注意:func此時繫結物件後,func變數的物件引用指標 指向繫結後
func = func.bind(Kirito);
func();

console.log("-------------------------------------------------------" + "

");

五、this指標顯式、隱式傳遞與繫結的優先順序

let priority = function () {
    console.log(this);
};

console.log("繫結優先於隱式");
Ausua.func_bind();
console.log("-------------------------------------------------------" + "

");

console.log("繫結優先於顯式");
priority = priority.bind("繫結");
priority.call("顯式");
priority();
console.log("-------------------------------------------------------" + "

");

console.log("顯式優先於隱式");
Ausua.func.call("顯式");
console.log("-------------------------------------------------------" + "

");

console.log("結論:優先順序:bind>顯示>隱式");

相關文章