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?
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.
__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.
[[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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With