可以覆盖window.console吗?它是只读的吗? [英] Can window.console be overwritten? Is it read-only?

查看:46
本文介绍了可以覆盖window.console吗?它是只读的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javascript中的 console 对象进行调试,并希望将其覆盖以在移动浏览器中使用此类功能.

I use the console object in javascript for debugging, and would like to overwrite it as to make use of such functionality in mobile browsers.

但是,我无法理解以下MDN文档

However , I have trouble understanding the following MDN documentation

Window.console只读属性返回对以下内容的引用:控制台对象,提供将信息记录到浏览器的控制台.

The Window.console read-only property returns a reference to the Console object, which provides methods for logging information to the browser's console.

( https://developer.mozilla.org/zh-CN/docs/Web/API/Window/console )

尤其是:

  1. 我无法对 < Window 接口,但它似乎与全局 window 属性
  2. 不同
  3. 一项测试表明(在当前的Chromium和Firefox中)window.console确实可以被覆盖,尽管这似乎与我在文档中阅读的内容相矛盾.(这样的覆盖示例提供了此答案;即使注释的对象是
  1. I cannot make head's or tails' of what actually is the Window interface, but it seems to be different to the global window property
  2. A test showed (in current Chromium and Firefox) that window.console can indeed be overwritten event though it seems to contradict what I read in the documentation. (An example of such an overwriting provides this answer; even though a comment objects with

您不能使用window.console = {...},因为window.console是只读属性!– Luillyfe 2月23日21:28

You can't use window.console = { ... } because window.console is a read-only property ! – Luillyfe Feb 23 at 21:28

Window.console window.console 的只读性是什么?

推荐答案

它不是只读的.MDN是一个很好的资源,但是它是由社区编辑的,有时会出现错误或无来源的声明.

It's not read-only. MDN is an excellent resource, but it's edited by the community and sometimes errors or unsourced claims sneak in.

  1. 我无法对 Window 界面的实际位置做任何改动,但它似乎与全局window属性不同
  1. I cannot make head's or tails' of what actually is the Window interface, but it seems to be different to the global window property

Window 接口定义了浏览器中的window对象上可用的属性和方法,可通过默认的 window 全局变量获得这些属性和方法.您可以这样认为: var window = new Window()当然不是 literally true.

The interface Window defines the properties and methods available on the window object in browsers, which is available via the default window global variable. You can think of it as this: var window = new Window() though of course that's not literally true .

  1. ... Window.console window.console 的只读性是什么?
  1. ... What is it with the read-only-ness of Window.console or window.console ?

它不是只读的.

  • Theory: The Window interface doesn't mention console at all, but the console spec says that it's a namespace object exposed on window. The WebIDL specification says that namespace objects are, by default, writable, configurable, and non-enumerable. (Thank you sideshowbarker for pointing that out!)

实践:在我尝试过的所有浏览器(Chrome,Firefox,Safari,IE9,IE11,Edge)上均可写.在除IE11之外的所有其他语言中, console 是对象 window 所引用的对象的自有"属性,并对其进行分配(在松散模式和严格模式下).(IE11很奇怪:关闭devtools时, console 不是 window 的自身属性,但是其类型为"object" 您登录 String(console)时,结果为"[object Console]" .但是当devtools打开时,它是 window 的自有属性而非只读.)

Practice: It's writable on every browser I've tried it on (Chrome, Firefox, Safari, IE9, IE11, Edge). In all of them except IE11, console is an "own" property on the object window refers to, and assigning to it works (in both loose and strict modes). (IE11 is the odd duck: console is not an own property of window when devtools are closed, but its type is "object" and if you log String(console) it the result is "[object Console]". But when devtools is open, it's an own property of window and not read-only.)

我已经更新了MDN页面,删除了该句子的只读"部分.

I've updated the MDN page, removing the "read-only" part of that sentence.

我并不是说用其他东西代替 console 一个好主意.:-)但是请注意,如果您选中了显示控制台"选项(默认情况下),那么SO自己的堆栈摘录就完全可以做到这一点.

I'm not saying that replacing console with something else is a good idea. :-) But note that SO's own Stack Snippets do exactly that if you have the "Show console" option checked (as it is by default).

这篇关于可以覆盖window.console吗?它是只读的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆