简体   繁体   中英

hiding the __proto__ property in Chrome's console

Whenever I type console.log/console.dir on an object, one of the properties that always shows up is __proto__ which is the constructor.

is there any way to hide this?

Redefine 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;
};

This won't modify the original object which definitely needs to have __proto__.

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);
};

var mixed = [1, [2, 3, 4], {'a': [5, {'b': 6, c: '7'}]}, [null], null, NaN, Infinity];
console.debug(mixed);

Use Opera and Dragonfly . In its settings (script tab), you can uncheck the option "Show Prototypes".

Although .__proto__ is important, there is obviously a way to hide things from the console. This is demonstrated when you try to do:

 for (var i in function.prototype) {
    console.log(i+": "+function.prototype[i].toString())
    }

There'll be some things that aren't logged to the console, and I think that is exactly what this whole topic is about (IMO answers should allow all prototypes so anybody visiting this topic can use it).

Also: The __proto__ is not the constructor. It is the object's highest priority prototype object. It is important that you don't delete the proto and leave it alone because if there is a method there that JavaScript relies on then the whole thing goes to shit. For the constructor look at obj.constructor , it doesn't screw with the whole damn thing and it may just be what it is named, the constructor, imagine that.

Hiding the __proto__ is not worth it! (see below why)

var old_log = console.log;
console.log = function ()
{
    function clearProto(obj)
    {
        if (obj && typeof(obj) === "object")
        {
            var temp = JSON.parse(JSON.stringify(obj));
            temp.__proto__ = null;
            for (var prop in temp)
                temp[prop] = clearProto(temp[prop]);
            return temp;
        }
        return obj;
    }
    for (var i = 0; i < arguments.length; ++i)
        arguments[i] = clearProto(arguments[i]);
    old_log.apply(console, arguments);
}

var mixed = [1, [2, 3, 4], {'a': [5, {'b': 6, c: {'d': '7'}}]}, [null], null, NaN, Infinity];
console.log([1, 2, 3], mixed);
old_log([1, 2, 3], mixed); // 4 comparison (original mixed has __proto__)

NaN and Infinity can't be cloned with JSON. In Chrome you also get the line-number on the right side of the dev tools. By overriding console.log you'll lose that. Not worth to hide __proto__ over that one!

Use Object.create(null) to create objects without __proto__

If you just want to hide the proto showing up on objects that have .__proto__ in console, you can't. Though I don't see why you would want to.

Btw, .__proto__ is not the object's constructor but its [[Prototype]] link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM