本文介绍了在javascript中为什么使用"var that = this"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript新手

hi i am new with javascript

使用此行有什么好处

var that = this

一个例子

function Person( firstname, lastname, age ) {

    this.firstname = firstname;

    this.lastname = lastname;

    this.age = age;

    getfullname = function() {

        return firstname + " " + lastname;
    };

    var that = this;


    this.sayHi = function() {

        document.write( "Hi my name is " + getfullname() + " and I am " + that.age + "years old.");

    };
}

谢谢

推荐答案

因为内部函数 this 与外部函数不同,因此将其别名为,您可以确保与同一个对象通话.

because in the inner function this will not be the same object as in the outer, so by aliasing it to that you can make sure you are talking to the same object.

这篇关于在javascript中为什么使用"var that = this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:36