Javascript function names in JSON?
Is it possible to encode J开发者_StackOverflow社区S function names in JSON?
JSON only has the 7 data types listed on the homepage for the project.
- Object
- Array
- String
- Number
- true
- false
- null
All JavaScript function names can be expressed as strings, so you can store one in a string without any further encoding.
Not sure, but maybe you are talking about namespacing?
A good example of this is: http://www.dustindiaz.com/namespace-your-javascript/
It has the appearance of JSON because JSON is formatted in a similar way. Although, it would not be classed as "encoding" your functions as JSON. But, rather creating functions within a namespaced structure (much like JSON).
Another example of this would be:
var YourNameSpace = {}; // This can be whatever
(YourNameSpace.utils = function() { // Function name (utils) can be whatever
return {
UtilityFunction:function(){
// Function Contents
alert('Im cool');
},
AnotherUtility:function(){
// Functions Contents
alert('Im cool too');
},
AnotherSetOfFunctions:function(){
return {
CoolFunction:function(){
// Function Contents
alert('Im even cooler!')
}
}
}()
}
}());
And to call those javascript functions:
YourNameSpace.utils.UtilityFunction(); //returns an alert: Im cool
YourNameSpace.utils.AnotherUtility(); //returns an alert: Im cool too
YourNameSpace.utils.AnotherSetOfFunctions.CoolFunction(); //returns an alert: Im even cooler!
So the above has the appearance of JSON as they are practically the same in structure.
Hope this helps, or at least gives you a new way to format your JS.
If you are talking about function names only, there is no problem as function's name is a string.
But if you are talking about serializing javascript object with member functions, it's not that easy.
However you can use JSONfn plugin
http://www.eslinstructor.net/jsonfn/
which lets you to stringify/parse javascript objects with member functions
Hope this helps,
-Vadim
精彩评论