Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 946
qwertymk Avatar asked Aug 05 '12 16:08

qwertymk


People also ask

What does __ proto __ mean in JavaScript?

Description. The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array.

Is __ proto __ deprecated?

__proto__ is considered outdated and somewhat deprecated (moved to the so-called “Annex B” of the JavaScript standard, meant for browsers only). The modern methods to get/set a prototype are: Object.

Which of the following has the special hidden property proto proto [[ prototype ]] prototype?

[[Prototype]] In JavaScript, objects have a special hidden property [[Prototype]] (as named in the specification), that is either null or references another object. That object is called “a prototype”: When we read a property from object , and it's missing, JavaScript automatically takes it from the prototype.


2 Answers

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__.

like image 54
Brian Ustas Avatar answered Oct 28 '22 14:10

Brian Ustas


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);
like image 39
Abdullah Avatar answered Oct 28 '22 14:10

Abdullah