将“this"引用分配给javascript中的变量 [英] Assign 'this' reference to a variable in javascript

查看:36
本文介绍了将“this"引用分配给javascript中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将this"引用分配给 javascript 中的变量.我想要的是:

Is it possible to assign 'this' reference to a variable in javascript. What I want is:

var someVariable = this;
alert(someVariable);

但我收到警告说未定义".

But I'm getting alert saying 'undefined'.

推荐答案

是的,可以将 this 分配给变量.当您获得 undefined 时,您可能会在定义变量的范围之外使用/警告该变量.

Yes, it is possible to assign this to a variable. When you get undefined, then you probably use/alert the variable outside the scope where it was defined.

这会起作用:

var someVariable;
function someFunction () {
    someVariable = this;
}
new someFunction();
alert(someVariable);

这篇关于将“this"引用分配给javascript中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆