javascript 静态方法中的Javascript“this”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11249037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript "this" in static methods
提问by Matteo Pagliazzi
I have a code like that:
我有一个这样的代码:
User = function(){}
User.a = function(){
return "try";
}
User.b = function(){
}
? From User.b() I can call User.a() using:
? 从 User.b() 我可以调用 User.a() 使用:
User.b = function(){
return User.a();
}
but not using this since it's not an instance of user (User.a() and User.b() are something like "static methods").
但不使用它,因为它不是用户的实例(User.a() 和 User.b() 类似于“静态方法”)。
What i want to do is to be able to call User.a() from User.b() without knowing which is the main function, in this case User.
我想要做的是能够从 User.b() 调用 User.a() 而不知道哪个是主函数,在这种情况下是 User。
Something like this to be used in static methods.
在静态方法中使用这样的东西。
回答by Esailija
In reality there is no methods or static methods in js, there's just functions that are assigned to object properties (functions being objects as well) and they all work the same way. Since you are calling it like User.b()
, this
will be User
for the call.
实际上,js 中没有方法或静态方法,只有分配给对象属性的函数(函数也是对象)并且它们的工作方式相同。由于您将其称为User.b()
,this
因此将User
用于呼叫。
User.b = function() {
return this.a();
}
回答by Guffa
The only thing that determines the context of the function is how you call it.
唯一决定函数上下文的是你如何调用它。
If you call it using a plain identifier (a function name, a variable or a property), the context will be the global window
object:
如果您使用普通标识符(函数名、变量或属性)调用它,则上下文将是全局window
对象:
someFunction();
If you call it using a period to access an object member, the context will be the object:
如果您使用句点调用它来访问对象成员,则上下文将是对象:
someObject.someFunction();
If you copy a member from an object to a variable, there is no connection to the object any more, and it will be called with window
as context:
如果将对象中的成员复制到变量,则不再与该对象连接,并且将使用window
上下文调用它:
var x = someObject.someFunction;
x();
If you assign a function as a property to an object, and call it using the object, the context will be the object:
如果将函数作为属性分配给对象,并使用该对象调用它,则上下文将是该对象:
someObject.x = someFunction;
someObject.x();
For your specific case, User
is a function, which also is an object.
对于您的特定情况,User
是一个函数,它也是一个对象。
If you call the function using User.b
, its context will be the User
object, which happens to be a function in this case. From within the function you can still use this
to access the context:
如果您使用 调用函数User.b
,它的上下文将是User
对象,在这种情况下恰好是一个函数。在函数中,您仍然可以使用它this
来访问上下文:
User.b = function(){
return this.a();
}
回答by Pablo Mescher
You don't have normal inheritance in javascript. I guess you are trying to do something like this:
您在 javascript 中没有正常的继承。我猜你正在尝试做这样的事情:
User = function(){
this.a= function(){
return 'try';
}
this.b= function(){
return this.a();
}
}
This way User becomes a constructor. Each new instance of User will have acces to these methods. So if you want to create a new instance of the User class, you can use the new keyword:
这样 User 就变成了构造函数。User 的每个新实例都可以访问这些方法。因此,如果要创建 User 类的新实例,可以使用 new 关键字:
var client= new User()
and then you'll have access to all the methods from user using client
然后您将可以使用客户端访问用户的所有方法
client.b() //returns 'try'
回答by Mark Reed
Well, functions exist independently of their container objects; they're just values. So ifyou're not calling them as methods on an object, they inherit whatever this
is in the calling context. In that case, expecting them to know about their container would be the same as assigning User.x
the value 1 and then expecting the number 1 to somehow know about User
.
好吧,函数独立于它们的容器对象存在;他们只是价值观。因此,如果您不将它们作为对象的方法调用,它们将继承this
调用上下文中的任何内容。在这种情况下,期望他们知道他们的容器与分配User.x
值 1 然后期望数字 1 以某种方式知道User
.
However, when you call User.a()
or User.b()
, you arein fact calling them as methods - methods of the (function object) User
. So this
will be the same as User
, and b
can just call this.a()
and you should be good to go.
然而,当你调用User.a()
or 时User.b()
,你实际上是在调用它们作为方法 - (function object) 的方法User
。So this
will 与 , 一样User
,b
可以调用this.a()
,你应该很高兴。