What is the difference between a JS object literal and a JSON string?

后端 未结 6 1808
灰色年华
灰色年华 2021-02-04 19:47

I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects, to me they seem similar:

{foo: \'bar\', bar : \'baz\'}
6条回答
  •  轮回少年
    2021-02-04 20:06

    Object Literal:

    Referencing mozilla ,

    An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

    Javascript Object:

    Referencing mozilla,

    In JavaScript, an object is a standalone entity, with properties and type

    JSON:

    Referencing mozilla and mozilla

    JSON (JavaScript Object Notation) is a data-interchange format. It closely resembles a subset of JavaScript syntax, although it is not a strict subset.JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON, and some JSON is not JavaScript.

    In loose words,

    An object is a javascript variable which can have properties and type.

    An object literal is a way to assign properties and associated values to object.

    JSON is a more strict object literal used for data interchange which is wrapped as string.

    (Usually, strictness is there to allow all languages to use it, we cannot use function as value, key should always be in double quotes (In object literal its not compulsory))

提交回复
热议问题