Is there any way to add method to JSON object?
有没有办法向JSON对象添加方法?
4 个解决方案
#1
12
Yes. Javascript functions are first class objects, which means you can attach them to other objects as properties.
是。 Javascript函数是第一类对象,这意味着您可以将它们作为属性附加到其他对象。
var myJson = { one: 1, two: 2 };
myJson.Sum = function() { return this.one + this.two; };
var result = myJson.Sum(); // result == 3
#2
1
It all depends on how you're using it.
这一切都取决于你如何使用它。
By using Tomas's function your actually storing Sum for EACH object you attach it too.
通过使用Tomas的函数,您实际存储了每个对象的Sum,您也可以将它附加。
If you save the function somewhere else before inserting inside your object you will use less memory.
如果在插入对象之前将函数保存在其他位置,则将使用较少的内存。
Bad one:
var array = [];
for (i=0; i<100000; i++)
array[i] = { test: function(){ "A pretty long string" }; }
Good one:
var array = [],
long = function(){ "A pretty long string"; };
for (i=0; i<100000; i++)
array[i] = { test: long }
In my tests the good one took ~3mb extra, the bad one took ~20mb extra. This happens because you're storing only 100000 references to a function, instead of 100000 anonymous functions.
在我的测试中,好的一个额外花了大约3mb,坏的花了大约20mb。这是因为您只存储了100000个函数的引用,而不是100000个匿名函数。
The approach you are referring to (the prototype based one) in your comment could be coded up as this:
您在评论中引用的方法(基于原型的方法)可以编码为:
function wrapper(data){
for (i in data)
this[i] = data[i]; // This is a fast hack
// You will probably want to clone the object, making sure nested objects and array
// got cloned too. In this way, nested objects and array will only get their
// reference copied inside this
}
wrapper.prototype.test = function(){
"A pretty long string";
}
var array = [];
for (i=0; i<100000; i++)
array[i] = new wrapper({})
Memory-wise is roughly the same as the Good one I wrote before but has the advantage of not polluting our object with the function name (test, in the example).
内存方式与我之前编写的Good方式大致相同,但具有不使用函数名称(示例中为test)污染对象的优点。
The disadvantages are mainly the length, the need to copy our data and the "hack" used to copy (which may be valid in certain designs and totally awful in other ones).
缺点主要是长度,复制数据的需要和用于复制的“黑客”(在某些设计中可能有效,在其他设计中可能非常糟糕)。
#3
0
Yes, it's just like any other object:
是的,它就像任何其他对象一样:
From the Chrome console:
从Chrome控制台:
> JSON.myMethod = function(x) { console.log(x); }
function (x) { console.log(x); }
> JSON.myMethod("hello")
hello
undefined
#4
0
It is simple ....
很简单 ....
var FillCbo =JSON.parse( '{"Category":""}');
FillCbo.Category = CboCategory;
Now Method Implimentation
现在方法实施
function CboCategory(PCboName)
{
Alert(PCboName);
}
Now You can Use FillCbo.Category() as a method anywhere in the Page
现在,您可以在页面中的任何位置使用FillCbo.Category()作为方法
FillCbo.Category("How Are Youu...");
#1
12
Yes. Javascript functions are first class objects, which means you can attach them to other objects as properties.
是。 Javascript函数是第一类对象,这意味着您可以将它们作为属性附加到其他对象。
var myJson = { one: 1, two: 2 };
myJson.Sum = function() { return this.one + this.two; };
var result = myJson.Sum(); // result == 3
#2
1
It all depends on how you're using it.
这一切都取决于你如何使用它。
By using Tomas's function your actually storing Sum for EACH object you attach it too.
通过使用Tomas的函数,您实际存储了每个对象的Sum,您也可以将它附加。
If you save the function somewhere else before inserting inside your object you will use less memory.
如果在插入对象之前将函数保存在其他位置,则将使用较少的内存。
Bad one:
var array = [];
for (i=0; i<100000; i++)
array[i] = { test: function(){ "A pretty long string" }; }
Good one:
var array = [],
long = function(){ "A pretty long string"; };
for (i=0; i<100000; i++)
array[i] = { test: long }
In my tests the good one took ~3mb extra, the bad one took ~20mb extra. This happens because you're storing only 100000 references to a function, instead of 100000 anonymous functions.
在我的测试中,好的一个额外花了大约3mb,坏的花了大约20mb。这是因为您只存储了100000个函数的引用,而不是100000个匿名函数。
The approach you are referring to (the prototype based one) in your comment could be coded up as this:
您在评论中引用的方法(基于原型的方法)可以编码为:
function wrapper(data){
for (i in data)
this[i] = data[i]; // This is a fast hack
// You will probably want to clone the object, making sure nested objects and array
// got cloned too. In this way, nested objects and array will only get their
// reference copied inside this
}
wrapper.prototype.test = function(){
"A pretty long string";
}
var array = [];
for (i=0; i<100000; i++)
array[i] = new wrapper({})
Memory-wise is roughly the same as the Good one I wrote before but has the advantage of not polluting our object with the function name (test, in the example).
内存方式与我之前编写的Good方式大致相同,但具有不使用函数名称(示例中为test)污染对象的优点。
The disadvantages are mainly the length, the need to copy our data and the "hack" used to copy (which may be valid in certain designs and totally awful in other ones).
缺点主要是长度,复制数据的需要和用于复制的“黑客”(在某些设计中可能有效,在其他设计中可能非常糟糕)。
#3
0
Yes, it's just like any other object:
是的,它就像任何其他对象一样:
From the Chrome console:
从Chrome控制台:
> JSON.myMethod = function(x) { console.log(x); }
function (x) { console.log(x); }
> JSON.myMethod("hello")
hello
undefined
#4
0
It is simple ....
很简单 ....
var FillCbo =JSON.parse( '{"Category":""}');
FillCbo.Category = CboCategory;
Now Method Implimentation
现在方法实施
function CboCategory(PCboName)
{
Alert(PCboName);
}
Now You can Use FillCbo.Category() as a method anywhere in the Page
现在,您可以在页面中的任何位置使用FillCbo.Category()作为方法
FillCbo.Category("How Are Youu...");