[英]hiding the __proto__ property in Chrome's console
每當我在對象上鍵入console.log/console.dir
時,總是出現的屬性之一是__proto__
,它是構造函數。
有什么辦法可以隱藏這個嗎?
重新定義 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;
};
這不會修改肯定需要有 __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);
使用歌劇和蜻蜓。 在其設置(腳本選項卡)中,您可以取消選中“顯示原型”選項。
盡管.__proto__
很重要,但顯然有一種方法可以從控制台隱藏內容。 當您嘗試這樣做時會證明這一點:
for (var i in function.prototype) {
console.log(i+": "+function.prototype[i].toString())
}
會有一些事情沒有記錄到控制台,我認為這正是整個主題的主題(IMO 答案應該允許所有原型,以便任何訪問該主題的人都可以使用它)。
另外: __proto__
不是構造函數。 它是對象的最高優先級原型對象。 重要的是不要刪除原型並將其單獨放置,因為如果那里有 JavaScript 依賴的方法,那么整個事情都會變得一團糟。 對於構造函數,看看obj.constructor
,它並沒有搞砸整個該死的東西,它可能只是它的名字,構造函數,想象一下。
隱藏__proto__
是不值得的! (原因見下文)
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
和Infinity
不能用 JSON 克隆。 在 Chrome 中,您還可以在開發工具的右側獲得行號。 通過覆蓋console.log
你會失去它。 不值得在那個上面隱藏__proto__
!
使用Object.create(null)
創建沒有__proto__
的對象
如果您只想隱藏顯示在控制台中具有.__proto__
的對象上的原型,則不能。 雖然我不明白你為什么要這樣做。
順便說一句, .__proto__
不是對象的構造函數,而是它的[[Prototype]] 鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.