gpt4 book ai didi

javascript - 使用窗口原型(prototype)覆盖所有窗口的 window.print() ?

转载 作者:行者123 更新时间:2023-11-27 23:56:58 25 4
gpt4 key购买 nike

在使用 Javascript 时,我尝试修改对象原型(prototype)。

对于String.toString(),这有效:

var testString = "Test";
testString.toString(); // returns "Test"
String.prototype.toString=function(){return "modified :-)";}
testString.toString(); // now returns "modified :-)"

但是,这不起作用:

Window.prototype.print=function(){
console.log("disabled");
}
window.print(); // still opens the print dialog

为什么这不起作用?window.print() 有什么特别之处吗?还是Window比较特殊?

注意:

这似乎只发生在 Firefox 中(在 V40 上测试)。在 Chrome 和 Internet Explorer 中,window.print() 按预期禁用。

这是 Firefox 的错误,还是有意为之?

最佳答案

我找不到任何官方文档,但似乎 window.print() 方法在 FF 内部受到保护。

但是,如果您愿意不使用 prototype,则以下内容在 Chrome 和 FF 中可以正常工作(但未在 IE 中进行测试):

window.print = function()
{
console.log('disabled');
}

window.print();

jsFiddle Demo

关于javascript - 使用窗口原型(prototype)覆盖所有窗口的 window.print() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32181838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com