this

Posted yasepix

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了this相关的知识,希望对你有一定的参考价值。

 

"use strict";

function Greet(name) {

    this.name = name;

    this.sayHello = function () {

        console.log("Hello " + this.name);

    };

}

var greet = new Greet("tom");

greet.sayHello();

输出:Hello tom

 

 

 

"use strict";

function Greet(name) {

    this.name = name;

    function sayHello() {

        console.log("Hello " + this.name);

    }

    return {

        name: "cat",

        sayHi: sayHello

    };

}

var greet = new Greet("tom");

greet.sayHi();

 

输出:Hello cat

 

 

"use strict";

function Greet(name) {

    var that = this;

    this.name = name;

    function sayHello() {

        console.log("Hello " + that.name);

    }

    return {

        name: "cat",

        sayHi: sayHello

    };

}

var greet = new Greet("tom");

greet.sayHi();

 

输出:Hello tom

 

以上是关于this的主要内容,如果未能解决你的问题,请参考以下文章

前端面试-闭包和作用域

请求拦截 XMLHttpRequest和fetch

构造函数

Vue中的$set的使用

this与super

java-this使用