[英]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.每当我在对象上键入console.log/console.dir
时,总是出现的属性之一是__proto__
,它是构造函数。
is there any way to hide this?有什么办法可以隐藏这个吗?
Redefine console.log:重新定义 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__.这不会修改肯定需要有 __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);
Although .__proto__
is important, there is obviously a way to hide things from the console.尽管.__proto__
很重要,但显然有一种方法可以从控制台隐藏内容。 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).会有一些事情没有记录到控制台,我认为这正是整个主题的主题(IMO 答案应该允许所有原型,以便任何访问该主题的人都可以使用它)。
Also: The __proto__
is not the constructor.另外: __proto__
不是构造函数。 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.重要的是不要删除原型并将其单独放置,因为如果那里有 JavaScript 依赖的方法,那么整个事情都会变得一团糟。 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.对于构造函数,看看obj.constructor
,它并没有搞砸整个该死的东西,它可能只是它的名字,构造函数,想象一下。
Hiding the __proto__
is not worth it!隐藏__proto__
是不值得的! (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. NaN
和Infinity
不能用 JSON 克隆。 In Chrome you also get the line-number on the right side of the dev tools.在 Chrome 中,您还可以在开发工具的右侧获得行号。 By overriding console.log
you'll lose that.通过覆盖console.log
你会失去它。 Not worth to hide __proto__
over that one!不值得在那个上面隐藏__proto__
!
Use Object.create(null)
to create objects without __proto__
使用Object.create(null)
创建没有__proto__
的对象
If you just want to hide the proto showing up on objects that have .__proto__
in console, you can't.如果您只想隐藏显示在控制台中具有.__proto__
的对象上的原型,则不能。 Though I don't see why you would want to.虽然我不明白你为什么要这样做。
Btw, .__proto__
is not the object's constructor but its [[Prototype]] link .顺便说一句, .__proto__
不是对象的构造函数,而是它的[[Prototype]] 链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.