JS对象文字和JSON字符串有什么区别? [英] What is the difference between a JS object literal and a JSON string?
问题描述
我对Object Literals,JSON,JavaScript Objects的具体含义感到困惑,对我来说,它们看起来很相似:
I have confusion about what exactly people mean by Object Literals, JSON, JavaScript Objects, to me they seem similar:
{foo: 'bar', bar : 'baz'}
AFAIK,上面是对象字面值,json以及javascript对象,不是吗?
AFAIK, above is object literal, json as well as javascript object, isn't it?
对象文字和json是否意味着同样的事情?
Does object literal and json mean the same thing ?
你们如何区分哪个是什么?
How do you guys differentiate which is what?
推荐答案
JSON 只是一种数据格式,像XML一样。 True JSON应该包含双引号括起来的键,如下所示:
JSON is a just a data format, like XML. True JSON should have the keys surrounded by double quotes, like so:
{"foo":"bar"}
JavaScript对象是JavaScript语言的一部分,并且具有相关的功能,例如原型。
JavaScript Objects are part of the JavaScript language, and have associated things such as a prototype.
对象文字正在创建一个带括号的javascript对象,而不是使用 new
keyword,或 Object.create()
。
Object literals is creating a javascript object in place with brackets as opposed to using the new
keyword, or Object.create()
.
//object literal
var foo = {};
//equivalent
var foo = new Object();
这篇关于JS对象文字和JSON字符串有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!