javascript - What does 'symbol-keyed' mean in `JSON.stringify` - Stack Overflow

There is a Object generated by Node.js, it looks like this when I use console.log:{ dataValues: { a: 1

There is a Object generated by Node.js, it looks like this when I use console.log:

{ dataValues: { a: 1, b: 2 }, fn1: function(){}, fn2: function(){} }

when I use JSON.stringify, it return this string:

{"a":1,"b":1}

I checked the mozilla developer center and found this:

All symbol-keyed properties will be pletely ignored, even when using the replacer function.

I think the 'dataValues' must be the 'symbol-keyed' property, but what does 'symbol-keyed' mean?

btw, I use the sequelizejs ORM lib to generate this object.

There is a Object generated by Node.js, it looks like this when I use console.log:

{ dataValues: { a: 1, b: 2 }, fn1: function(){}, fn2: function(){} }

when I use JSON.stringify, it return this string:

{"a":1,"b":1}

I checked the mozilla developer center and found this:

All symbol-keyed properties will be pletely ignored, even when using the replacer function.

I think the 'dataValues' must be the 'symbol-keyed' property, but what does 'symbol-keyed' mean?

btw, I use the sequelizejs ORM lib to generate this object.

Share Improve this question edited Nov 18, 2021 at 13:33 Yves M. 31.1k24 gold badges109 silver badges149 bronze badges asked Aug 9, 2014 at 9:22 TomWanTomWan 3292 silver badges9 bronze badges 4
  • 1 "{ 'a': 1, 'b': 1}" is invalid JSON. I can't see how JSON.stringify could have produced that. How did you use it? – John Dvorak Commented Aug 9, 2014 at 9:26
  • sorry, the string is wrong, I have modified it to right format. – TomWan Commented Aug 9, 2014 at 9:32
  • @TomWan: If you use JSON.stringify on what you've quoted, you will not get {"a":1,"b":1}, you'll get {"dataValues":{"a":1,"b":2}}; proof: jsbin./ralova/1 – T.J. Crowder Commented Aug 9, 2014 at 9:33
  • I wrote JavaScript for 3 years, I know it is impossible in browser, It happend in NodeJS, you can write some code with NodeJS and SequelizeJS and get some data from MySQL and see how it returns by Promise. – TomWan Commented Aug 9, 2014 at 9:41
Add a ment  | 

2 Answers 2

Reset to default 4

I found the reason finally in the same page:

If an object being stringified has a property named toJSON whose value is a function, then the toJSON method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON method when called will be serialized.

It runs on browser normally. Here is the jsfiddle to run it like I asked.

Code:

function test(data) {
    for(var key in data){
        this[key] = data[key];
    }
}

test.prototype.toJSON = function(){
    return this.dataValues;
}

var a = new test({dataValues: {a:1, b:2}});

console.log(a);//the instance
console.log(JSON.stringify(a));//{"a":1,"b":1}

Nah, the relevant part to your issue is this blurb:

If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).

So in other words, if your JSON object contains functions, they are omitted during the JSON.stringify process.

"symbol-keyed" refers to a new primitive type introduced in ecmascript6. See https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/typeof for more info.

This is an example of a "symbol-keyed" property:

{[Symbol("foo")]: "foo"}

Reference for JavaScript Symbol: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745407807a4626392.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信