“这个”内部对象
我试图根据通过请求传入的宽度(默认为 560)计算比例高度(同时排除静态高度元素)。
但是,wF.h
的计算结果为 NaN
。如果我将 this.w
替换为 560,它会起作用,但在尝试引用 wF
的 w
属性时则不起作用。
var wF = {
w : 560,
h : (312 - 42) / (560 / this.w) + 42
};
什么给?
我拒绝连续使用两个普通变量,因为我试图从 JS 中获得好的代码。
更新:
感谢所有帮助解释和解决我的问题的人。我想我必须习惯这一点。我将分阶段设置该对象以继续该项目,尽管它仍然让我有点烦恼;)。我发现并阅读了一篇关于该主题的好文章,供任何偶然发现类似问题的人使用:http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
I'm trying to calculate a proportional height (while excluding a static height element) from a width that gets passed in via a request (defaults to 560).
However, wF.h
evaluates to NaN
. If I replace this.w
with 560 it works, but not when trying to reference the w
property of wF
.
var wF = {
w : 560,
h : (312 - 42) / (560 / this.w) + 42
};
What gives?
I refuse to use two plain vars in succession, because I'm trying to get nice code out of JS.
Update:
Thanks to everyone who helped explain and solve my problem. I guess i'll just have to get used to that. I'll be setting the object up in stages to get on with the project, even though it still annoys me slightly ;). I found and read a nice article on the topic for anyone who stumbles upon similar issues: http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
this
并不是你想象的那样Javascript 没有块作用域,只有函数作用域:
wF
定义中的this
确实不参考wF
。(因此,无论
this
是什么,this.w
都可能是undefined
。除以undefined
得到NaN
。)那么你可能会尝试:
你还没有完成对象的定义但是
,你仍在定义对象,你尝试使用
wF.w< /代码>: 还没有准备好。
解决方案
所以,是的,您将必须使用两个变量...或分阶段设置对象:
this
isn't what you think it isJavascript has no block scope, only function scope:
this
inside the definition forwF
does not refer towF
.(And so
this.w
, whateverthis
is, is likelyundefined
. Dividing byundefined
yieldsNaN
.)So then you might try:
You haven't finished defining the object yet
However, you're still defining the object where you attempt to use
wF.w
: it's not ready for that yet.Solution
So, yes, you will have to use two variables... or set up the object in stages:
您好,只需将您的第二个属性重新定义为函数对象,它就会起作用。我认为可以从函数内访问调用对象的上下文
Hi just redefine your second property as a function object and it will work. I think it is possible to access the context of the calling object from within a function
this
关键字指的是调用上下文,而不是对象。您需要分两步执行此操作,如下所示:
The
this
keyword refers to the calling context, not an object.You need to do this in two steps like so:
您不需要将 {...} 包装在 Object() 中。它已经是一个对象字面量。
this
不在对象字面量内部进行操作,它将指向该函数当前正在其中运行的对象,因此:将导致
this
指向window对象。
编辑:以前的代码并不是要作为答案,只是演示
this
是什么。另一种可能性是使用一个将宽度作为参数的函数:You don't need to wrap the {...} in an Object(). It is already an object literal.
this
doesn't operate inside the object literal, it will point to the object that the function is currently running in so:will cause
this
to point to thewindow
object.EDIT: Previous code was not intended to be an answer, just a demonstration of what
this
is. Another possiblity would be to use a function that takes the width as an argument:当您在 JSON 对象声明中使用 this 时,它指向当前实例。
您可以在控制台中尝试以下代码以更好地理解它:
When you use this in a JSON object declaration, it points to the current instance.
You can try this code in your console to understand it better: