[英]Access calling object from Javascript method
In JavaScript, for an anonymous function defined inside the object, the this
keyword refers to the object itself. 在JavaScript中,对于在对象内部定义的匿名函数, this
关键字引用对象本身。 However, when the function is defined outside the object scope, this
refers to the window object, example: 但是,当函数在对象范围之外定义时, this
引用的是窗口对象,例如:
function foo() { console.log(this) } let A = { att: foo(), att2: "bla" } A.foo();
[EDIT]: I made a mistake in the original code, I meant to call the function as A.att() (and I am obliged to keep att: foo(). My code is basically the following: [编辑]:我在原始代码中犯了一个错误,我打算将该函数称为A.att()(并且我必须保留att:foo()。我的代码基本上是以下内容:
function foo() { console.log(this) } let A = { att: foo(), att2: "bla" } A.att();
The output here is object Window, and not the object that I want to access, which is A. And I can't change A, so how can I access A from foo()? 这里的输出是对象Window,而不是我要访问的对象,它是A。而且我不能更改A,那么如何从foo()访问A?
A function's
this
keyword behaves a little differently in JavaScript compared to other languages. 与其他语言相比,函数的this
关键字在JavaScript中的行为略有不同。 It also has some differences between strict mode and non-strict mode. 在严格模式和非严格模式之间也有一些区别。 In most cases, the value ofthis
is determined by how a function is called. 在大多数情况下,值this
由函数是如何被调用来确定。 It can't be set by assignment during execution, and it may be different each time the function is called. 在执行过程中不能通过赋值来设置它,并且每次调用该函数时可能会有所不同。
To manipulate the this
object by yourself you can use the JS methods call
, apply
& bind
. 要自己操纵this
对象,可以使用JS方法call
, apply
& bind
。
Lets demonstrate each with example: 让我们用示例进行演示:
Function.prototype.call() Function.prototype.call()
Detailed info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind 详细信息: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
function foo() {
console.log(this)
}
var A = {
att: foo,
att2: "bla"
}
foo.call(A);
Function.prototype.apply() Function.prototype.apply()
Detailed info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply 详细信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
function foo() {
console.log(this)
}
var A = {
att: foo,
att2: "bla"
}
foo.apply(A);
Function.prototype.bind() Function.prototype.bind()
Detailed info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind 详细信息: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
function foo() {
console.log(this)
}
var A = {
att: foo,
att2: "bla"
}
var x = foo.bind(A);
x();
In JavaScript, for an anonymous function defined inside the object, the
this
keyword refers to the object itself. 在JavaScript中,对于在对象内部定义的匿名函数,this
关键字引用对象本身。 However, when the function is defined outside the object scope,this
refers to the window object 但是,当函数在对象范围之外定义时,this
是指窗口对象
No. Where the function is defined is irrelevant. 否。函数的定义位置无关紧要。 It is how a function is called that determines the value of this
. 函数的调用方式决定了this
的值。
The output here is object Window, and not the object that I want to access, which is A. 这里的输出是对象Window,而不是我要访问的对象A。
That is because you are calling foo()
and then assigning the return value to att
. 那是因为您要调用foo()
,然后将返回值分配给att
。 this
is window
because you aren't calling foo()
with any context. this
是window
因为您没有在任何上下文中调用foo()
。
Later you call A.foo();
稍后您调用A.foo();
which throws an error because A.foo()
is undefined
. 因为A.foo()
是undefined
,所以抛出错误。
Change: att: foo()
to att: foo
so you assign the function instead of calling it, and change A.foo()
to A.att()
更改: att: foo()
到att: foo
以便您分配函数而不是调用它,然后将A.foo()
更改为A.att()
function foo() { console.log(this) } let A = { att: foo, att2: "bla" } A.att();
I would say I'm not agree with the answer because the question here is how to get the this (object) from where I'm calling the function, that is actually the Object A. I would prefer to bind the Object function instead to bind the Object directly to the global function. 我会说我不同意答案,因为这里的问题是如何从调用函数的地方(实际上是对象A)获取this(对象)。我宁愿将Object函数绑定到将对象直接绑定到全局函数。 See the example below is binding A as the this object for B. In this situation I would be available to do this with any number of Objects without have to affect the original function. 请参见下面的示例,将A绑定为B的this对象。在这种情况下,我可以使用任意数量的Object来执行此操作,而不必影响原始功能。
function foo() { console.log(this) } let A = { att: foo, att2: "From A" } let B = { att: foo, att2: "From B" } B.att = B.att.bind(A) A.att(); B.att()
function foo() {
console.log(this)
}
let A = {
att: foo(),
att2: "bla"
}
foo.call(A); //It will return Object A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.