Why doesn't JSON.stringify display object properties that are functions?

前端 未结 3 1378
盖世英雄少女心
盖世英雄少女心 2020-12-16 03:28

Why doesn\'t JSON.stringify() display prop2?

var newObj = {
  prop1: true,
  prop2: function(){
    return \"hello\";
  },
  prop3: false
};

alert( JSON.str         


        
3条回答
  •  生来不讨喜
    2020-12-16 04:30

    Here is another way with using a .prototype. You can add an function to stringify

    JSON.stringify(obj, function(k, v) {
      if (typeof v === 'function') {
        return v + '';
      }
      return v;
    });
    

提交回复
热议问题