“这个”内部对象

发布于 2024-11-29 08:00:01 字数 685 浏览 1 评论 0原文

我试图根据通过请求传入的宽度(默认为 560)计算比例高度(同时排除静态高度元素)。

但是,wF.h 的计算结果为 NaN。如果我将 this.w 替换为 560,它会起作用,但在尝试引用 wFw 属性时则不起作用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素染倾城色 2024-12-06 08:00:01
// Your code
var wF = {
       w : 560,
       h : (312 - 42) / (560 / this.w) + 42
};

this 并不是你想象的那样

Javascript 没有块作用域,只有函数作用域:wF 定义中的 this 确实参考wF

(因此,无论 this 是什么,this.w 都可能是 undefined。除以 undefined 得到 NaN。)

那么你可能会尝试:

// Let's not use `this`
var wF = {
       w : 560,
       h : (312 - 42) / (560 / wF.w) + 42
};

你还没有完成对象的定义但是

,你仍在定义对象,你尝试使用wF.w< /代码>: 还没有准备好


解决方案

所以,是的,您将必须使用两个变量...或分阶段设置对象:

// We can't even use `wF`; split up the property definitions
var wF = {};
wF.w = 560;
wF.h = (312 - 42) / (560 / wF.w) + 42;
// Your code
var wF = {
       w : 560,
       h : (312 - 42) / (560 / this.w) + 42
};

this isn't what you think it is

Javascript has no block scope, only function scope: this inside the definition for wF does not refer to wF.

(And so this.w, whatever this is, is likely undefined. Dividing by undefined yields NaN.)

So then you might try:

// Let's not use `this`
var wF = {
       w : 560,
       h : (312 - 42) / (560 / wF.w) + 42
};

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:

// We can't even use `wF`; split up the property definitions
var wF = {};
wF.w = 560;
wF.h = (312 - 42) / (560 / wF.w) + 42;
自由如风 2024-12-06 08:00:01

您好,只需将您的第二个属性重新定义为函数对象,它就会起作用。我认为可以从函数内访问调用对象的上下文

var wF = {
    w : 560,
    h : function() { return (312 - 42) / (560 / this.w) + 42; }
};

alert(wF.h())

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

var wF = {
    w : 560,
    h : function() { return (312 - 42) / (560 / this.w) + 42; }
};

alert(wF.h())
命比纸薄 2024-12-06 08:00:01

this 关键字指的是调用上下文,而不是对象。

您需要分两步执行此操作,如下所示:

var wF = { w: 560 };
wF.h = (312 - 42) / (560 / wF.w) + 42;

The this keyword refers to the calling context, not an object.

You need to do this in two steps like so:

var wF = { w: 560 };
wF.h = (312 - 42) / (560 / wF.w) + 42;
不必了 2024-12-06 08:00:01

您不需要将 {...} 包装在 Object() 中。它已经是一个对象字面量。

this 不在对象字面量内部进行操作,它将指向该函数当前正在其中运行的对象,因此:

function fn() {
   var wF = { w : 560, h : (312 - 42) / (560 / this.w) + 42 };
}

fn();

将导致 this 指向 window对象。

编辑:以前的代码并不是要作为答案,只是演示 this 是什么。另一种可能性是使用一个将宽度作为参数的函数:

function getObject(width) {
    width = width || 560; //Default value to 560
    return { w: width, h : (312 - 42) / (560 / width) + 42 };
}

var wF = getObject(); //To get the default value, or specify a width otherwise.

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:

function fn() {
   var wF = { w : 560, h : (312 - 42) / (560 / this.w) + 42 };
}

fn();

will cause this to point to the window 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:

function getObject(width) {
    width = width || 560; //Default value to 560
    return { w: width, h : (312 - 42) / (560 / width) + 42 };
}

var wF = getObject(); //To get the default value, or specify a width otherwise.
征﹌骨岁月お 2024-12-06 08:00:01

当您在 JSON 对象声明中使用 this 时,它指向当前实例。

您可以在控制台中尝试以下代码以更好地理解它:

Object({ w : 560, h : (312 - 42) / (560 / function(e){console.log(e);return e.w;}(this)) + 42 });

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:

Object({ w : 560, h : (312 - 42) / (560 / function(e){console.log(e);return e.w;}(this)) + 42 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文