0
func=function() {foo=true}
alert(JSON.stringify(func));

alerts "undefined"

obj={foo: true}
alert (JSON.stringify(obj));

alerts: "{foo: true}"

Why is this? As far as I understand JSON.stringify() converts an object to a json string, but I notice this doenst work for a "function object".

Btw searching for "function object" is hell in search engine land, because the only results I get out of google are about how objects are functions and functions are objects, but little about an actual function object.

"The Function constructor creates a new Function object. In JavaScript every function is actually a Function object." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

"Ok google: so every javascript function is an object, and every object is an function..." its the same, but it acts differenlty? I dont get it. So I am turning to stackoverflow. Can anyone enlighten me and explain why JSON.stringify(function() {foo=true}) does not work?

(ps. my goal is to turn function() {foo=true} into "function() {foo=true}" I am not looking for a workaround. I want to know why it doenst work to convert the function object to (json) string.

2 Answers 2

3

You can use Function.prototype.toString()

var a = function() { return 1; };

console.log( a.toString() ); // "function() { return 1; }"

Im not sure about the support though.

Sign up to request clarification or add additional context in comments.

3 Comments

It's been around since at least ed 1 so should be fine.
Thanks for that example. I wasnt aware toString() could work :)
@058WistWol Note that you cannot use .toString() on native functions like alert, console.log, etc: alert.toString() -> function alert() { [native code] }"
1

Reason: JSON --> JavaScript Object Notation. JSON is a mechanism to persist (serialize) the state of a JavaScript object in a String so that it can be reused later / sent to server / process in other way.

function is not an object --> so its state cannot be serialized.

The return value of a function can however be serialized in case the return is a non-function value.

5 Comments

"function is not an object". Yes it is, however there is no serialisation for functions in the JSON grammar.
@RobG think in terms of any programming language, a function is associated with the behavior it does and not the characteristics it holds. So calling function an object is wrong.
"The Function constructor creates a new Function object. In JavaScript every function is actually a Function object." developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (they write "function" in sub, but still its really confusing.. is there a special function object and are functions==objects or are functions===objects or perhaps still function!==object) it doenst really mather. But still huh
A Function is an Object is JavaScript, so this answer is not as clear as it could be. To see this in action try: var x = function() { return 42; };, x instanceof Function // true, x instanceof Object // true. The correct answer has been mentioned by @RobG, that is the JSON specification does not describe how to serialise Functions.
"think in terms of any programming language" - You can't just assume all programming languages will work the same way. Some programming languages don't have functions. Some don't have objects. In JS a function is definitely an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.