0

I recently found out that JSON values can only store string, number, object, array, true, false or null. But from my understanding, JSON is how Javascript represents its objects internally. I don't understand how it is possible to store Javascript objects as JSON if most objects have methods, which are functions? Aren't functions objects? What the heck are functions in my Javascript interpreter's (Node.js) opinion and how does it represent them? Thanks!

9
  • 4
    But from my understanding, JSON is how Javascript represents its objects internally. No, that's not correct. JSON is nothing more than a data-transport string format. Internally, objects are stored as hashtables of key/value pairs and the values can be any legal JavaScript primitive or object type. Commented Nov 23, 2018 at 21:02
  • 1
    In addition to @ScottMarcus comment JSON is not a JS Object. It's notation in a similar fashion of a JS Object. They're two separate things. Commented Nov 23, 2018 at 21:02
  • @ScottMarcus Thank you! I want to see what the internal hashtable of a JS function actually looks like. Can I tell JS via my Node.js console to show me the hashtable of a function I make? Commented Nov 23, 2018 at 21:07
  • You can't. That's not something that is defined by the ECMAScript standard and is implementation-specific. You'd have to get into the source code of each client and see how they approach it. And frankly, why do you need to? The whole point of any programming language (besides assembly) is to abstract such things. Commented Nov 23, 2018 at 21:08
  • @ScottMarcus I don't need to, it's just for fun Commented Nov 23, 2018 at 21:09

1 Answer 1

3

JSON is a string interchange format. It stands for JavaScript Object Notation. It was invented long after Javascript and has nothing at all to do with how Javascript stores data internally.

JSON is typically used as an interchange format or storage format. One would take some Javascript data, serialize it to the JSON format and take the resulting string and send it to another process or computer or save it in some sort of storage.

The recipient of the JSON can then parse it back into whatever their local data is. JSON is even used to send data from a Javascript program to a program written in another language (Python, Ruby, C++, etc...).

Functions have no connection at all to JSON. They are not stored in JSON. Their internal storage format inside the JS interpreter is specific to whatever interpreter implementation and is not accessible to the outside world or governed by any standard. It's an implementation detail for any Javascript engine and they can do it however they want and each interpreter likely has it's own implmentation or variation. I don't know of any reason why it would matter to your Javascript code.

I recently found out that JSON values can only store string, number, object, array, true, false or null. But from my understanding, JSON is how Javascript represents its objects internally.

That is not correct. JSON is not something that the Javascript interpreter uses for its objects internally. Internal object formats are specific to a particular Javascript interpreter and are not accessible to Javascript code, nor really relevant when writing code.

I don't understand how it is possible to store Javascript objects as JSON if most objects have methods, which are functions?

Javascript does not use JSON for internal storage so it has nothing at all to do with the internal implementation of Javascript data types.

Aren't functions objects?

Yes, but they have nothing to do with JSON.

What the heck are functions in my Javascript interpreter's (Node.js) opinion and how does it represent them?

Each JS interpreter has its own internal implementation/storage for functions. It is not governed by any standard and is largely irrelevant to how you write code in Javascript.


If you had some reason to want to know how a specific Javascript implementation stores its variables internally, you would have to look into the source code. The V8 implementation from Google (used in Chrome and node.js) and the Firefox implementation from Mozilla are both open source and you could dive into that code (it would be mostly C++ code).

This can get pretty complicated because some data types such as Arrays are stored in a variety of different formats depending upon the structure of the array. I believe V8 has at least three storage formats for arrays depending upon whether the array is compacted or sparse and based on its overall size. This is to optimize for both memory consumption and run-time performance.

Likewise properties on objects may be arranged in highly optimized storage formats if the interpreter has advance information from the code about what is being used and what is not, compared to arbitrary programmatically generated properties.

FYI, you can find the Google repository here: https://chromium.googlesource.com/v8/v8.git and the Mozilla code here: https://hg.mozilla.org/.

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

Comments

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.