开发者

What kind of type is this statement?

开发者 https://www.devze.com 2023-04-07 18:05 出处:网络
In javascript : is it legal ? var obj = [ id: 开发者_如何转开发\'1\', name: \'\' ]; type typeof(obj) return n/aThat is a syntax error in JavaScript.Probably it should be:

In javascript : is it legal ?

 var obj = [ id: 开发者_如何转开发'1', name: '' ]; 

type typeof(obj) return n/a


That is a syntax error in JavaScript. Probably it should be:

var obj = { id: '1', name: '' };

That's an object literal. An array literal looks like this:

var arr = [ 1, 2, 3 ];

You can put objects inside of arrays too:

var objarr = [ { id: '1', name: '' }, { id: '2', name: 'example' } ];

An empty object looks like:

var emptyObj = {};

An empty array looks like:

var emptyArr = [];


I guess you want an object (looking at your variable name). In that case it would be:

var obj = { id: '1', name: '' }; 

The [ and ] tokens are used to define an array and must look like:

var arr = ['a', 'b', 'c'];

If you would like to know more about this in context to JSON, look at JSON


Square brackets denotes an array, curly ones an object:

var obj = []; // short form to declare an array
var onj = {}; // short form to declare an object
0

精彩评论

暂无评论...
验证码 换一张
取 消