JS Proxy Pattern

筅森魡賤 提交于 2019-12-24 03:41:41

问题


I use this code to override the window.alert function. The function replaces breaks by \r\n. It works fine in Firefox, but ofcourse not in IE. Im getting the error: Property or method not supported.

(function() {
  var proxied = window.alert;
  window.alert = function(txt) {
    txt = txt.replace(/<br>/g, "\r\n");
    return proxied.apply(this, arguments);
  };
})();

Please help me find the solution! Thank you


回答1:


I would do this, in case window.alert is not a "real" function in IE:

(function() {
  var proxied = window.alert;
  window.alert = function(txt) {
    txt = txt.replace(/<br>/g, "\r\n");
    return proxied(txt);
 };
})();

Sorry, untested, Does it work?




回答2:


This would be fine for native JavaScript functions but is highly dangerous with methods of host objects such as window. Host objects are not subject to the normal rules of native JavaScript objects, and can (and do) behave largely as they please, often differently in different browsers. Therefore I strongly recommend not pursuing this idea for window.alert or any other host methods.



来源:https://stackoverflow.com/questions/3060318/js-proxy-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!