在setTimeout()中使用变量时,变量显示为未定义

时间:2018-06-08 05:35:30

标签: javascript undefined settimeout

$(document).ready(function() {1
function Test() {
    this.testArray = ["hi", "bye", "ok", "lol"];

    this.doStuff = function() {
        //returns the value of the array
        console.log(this.testArray);

        setTimeout(function() {

            //returns undefined.
            console.log(this.testArray);

        }, 0.12)
    }
}
//instantiating a Test() object.
var t = new Test();

function update() {
    //calls doStuff() on object t every 100 ms.
    t.doStuff();
}

setInterval(update, 100);

console.log(this.testArray)正文中包含setTimeout语句时,为什么此脚本会将this.testArray作为undefined返回,但它会返回console.log(this.testArray)时分配的值{1}}语句在setTimeout正文之外?

0 个答案:

没有答案