隐藏Chrome控制台中的__proto__属性

qwe*_*ymk 12 javascript debugging console google-chrome

每当我输入console.log/console.dir一个对象时,总会出现的一个属性__proto__就是构造函数.

有没有办法隐藏这个?

Bri*_*tas 7

重新定义console.log:

console.log = function (arg) {
    var tempObj;

    if (typeof arg === 'object' && !arg.length) {
        tempObj = JSON.parse(JSON.stringify(arg));
        tempObj.__proto__ = null;
        return tempObj;
    }

    return arg;
};
Run Code Online (Sandbox Code Playgroud)

这不会修改肯定需要__proto__的原始对象.

  • 我试过了,但控制台现在不再显示了任何东西:S (5认同)
  • 这将具有有限的用途。不会显示非 JSON 友好的值。 (2认同)

Abd*_*dın 5

console.debug = function() {
  function clear(o) {

    var obj = JSON.parse(JSON.stringify(o));
    // [!] clone

    if (obj && typeof obj === 'object') {
        obj.__proto__ = null;
        // clear

        for (var j in obj) {
          obj[j] = clear(obj[j]); // recursive
        }
    }
    return obj;
  }
  for (var i = 0, args = Array.prototype.slice.call(arguments, 0); i < args.length; i++) {
    args[i] = clear(args[i]);
  }
  console.log.apply(console, args);
};
Run Code Online (Sandbox Code Playgroud)
var mixed = [1, [2, 3, 4], {'a': [5, {'b': 6, c: '7'}]}, [null], null, NaN, Infinity];
console.debug(mixed);
Run Code Online (Sandbox Code Playgroud)

  • 我把它收回 !!!!不要这样做!!!!它确实弄乱了对象 - 只花了一整天的时间与 select2 战斗 - 绝望地从 console.debug 切换到 console.log - 我所有的麻烦都消失了:) (2认同)