Javascript 对象文字表示法 - 创建 mixin(某种程度)
由于缺乏更好的标题...是否可以在 JSON 中执行类似的操作?
var obj = {
a : 'a constant',
b : a + ' and b constant'
}
更新:不起作用 - this.a 返回未定义
我知道这会起作用,但它看起来相当难看且令人费解:
var obj = {
a : 'a constant',
b : (function(){ return this.a + ' and b constant'; })()
}
< /罢工> 这就是我目前所掌握的:
var obj = {
a : 'a constant',
b : 'a constant and b constant'
}
我知道一些人评论过的其他方式,我真诚地感谢他们的回应。然而,它们看起来并不简单或优雅。我当前实现中遇到的唯一问题是,如果“a”的值被更新,则必须在许多地方执行搜索-n-替换(我说很多是因为有更多属性源自“a”)而不是一。
For lack of a better title... Is it possible to do something like this in JSON?
var obj = {
a : 'a constant',
b : a + ' and b constant'
}
Update: Doesn't work - this.a returns undefined
I know this will work but it looks rather ugly and convoluted:
var obj = {
a : 'a constant',
b : (function(){ return this.a + ' and b constant'; })()
}
And here's what I have currently:
var obj = {
a : 'a constant',
b : 'a constant and b constant'
}
I know of the other ways that some have commented upon and I sincerely thank them for responding. However, they don't look simple or elegant. The only issue I have with my current implementation is that if value of 'a' ever gets updated, search-n-replace has to done in many places (I say many because there are more properties that derive from 'a') instead of one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它不漂亮,也不是 JSON,但我认为这是你唯一的希望。
否则,请以不同方式标记 JSON 以适合您的用例:
It's not pretty, and it's not JSON, but I think it's your only hope.
Otherwise, mark up the JSON differently to fit your use case:
据我所知,简短的答案是否定的,尽管有很多方法可以使用更多涉及的代码来做到这一点。显而易见的方法是将
a
放入变量中:在定义对象时,需要调用基于函数的方法,而不是求值:
另一种方法(也可能说明了显而易见的问题)是两个-通行证版本:
As far as I know, the short answer is no, though there are many ways to do this with more involved code. The obvious one is to put
a
into a variable:The function-based approach would need to be called, not evaluated when you define the object:
The other approach, also probably stating the obvious, is a two-pass version:
找到了一个潜在的解决方案:
感谢大家的提示!赞成所有好的建议。
Found a potential solution:
Thanks everyone for their tips! Upvoting all good suggestions.
在Json,不。 http://json.org/
你的意思是在javascript中吗?
In JSON, no. http://json.org/
Did you mean in javascript?
JSON 是一种数据格式。它没有函数的概念,就像任何其他数据格式(HTML、XML、纯文本)一样。
这似乎唯一的目的是数据压缩。为什么不直接使用众多标准实现之一来压缩数据呢?
如果您希望您的数据具有大量冗余,而这种冗余无法通过正常的压缩方案进行有效压缩,那么您可以执行您在此处讨论的操作,例如,
两端的代码都必须实现和解析这。也就是说,它将在每个成员中搜索
[fld=x]
,并替换为成员x
的值。但它仍然只是一个自定义的数据压缩方案。它与 JSON 无关。编辑
Javascript 对象
和JSON
是完全不同的(尽管相关)事物。 Javascript 对象是 Javascript 中的任何实体。 JSON 是一种数据格式,旨在以基于文本的表示法表示编程对象,特别是为了通过线路与其他编程实体进行交换(或者当内部格式不可移植时)。如果您只是问 Javascript 对象是否可以包含引用其另一个成员的函数,那么当然,您已经回答了自己的问题!
不过,您不需要自动执行的函数,它只会创建工作,并且最终也不会反映将来对 A 所做的任何更改。只需执行一个常规函数即可:
b : function() { return this.a + ' and bconstant'; };
或者更有可能的是,只需创建一个“方法”并保留您的字段:
JSON is a data format. It has no concept of functions, just like any other data format (HTML, XML, plain text).
The only purpose this would seem to serve is data compression. Why don't you just compress the data using one of the many standard implementations out there instead?
If you expect your data to have massive redundancies of a sort that would not be efficiently compressed by a normal compression scheme, then you could do something like you're talking about here, e.g.
and your code at either end would have to implement and parse this. That is, it would search for
[fld=x]
in each member and substitute with the value of memberx
. But it's still just a custom data compression scheme. It has nothing to do with JSON.edit
Javascript Objects
andJSON
are entirely different (albeit related) things. A Javascript object is any entity in Javascript. JSON is a data format that is meant to represent the programmatic object in a text-based notation, specifically for the purpose of exchange with other programmatic entities across a wire (or when the internal format is otherwise not portable).If you are just asking if a Javascript object can contain a function that refers to another one of it's members, then sure, you already answered your own question!
You don't need a self-executing function though, that just creates work, and it would also end up not reflecting any changes made to A in the future. Just do a regular function:
b : function() { return this.a + ' and b constant'; };
or more likely, just create a "method" and leave your fields alone:
好的,这是一种方法。我不知道这是否适合您,因为它不会递归,但是请尝试一下。这利用 reviver 函数来操作数据。
请注意,原始字符串是您请求的有效 JSON 字符串。正如其他人提到的,您的示例不是有效的 JSON。
示例
编辑
另外,我在 Opera 11、IE8 和 FF 3.6 上对此进行了测试。
Ok, here's a WAY to do it. I don't know if this will work for you since it doesn't recurse, however give it a try. This makes use of the reviver function to manipulate the data.
Note that the original string is a valid JSON string as you requested. As others have mentioned your examples are not valid JSON.
Example
Edit
Also, I tested this on Opera 11, IE8 and FF 3.6.