如何在javascript中代理函数?
我是javascript的新手。所以我不知道我要找的名字是什么,我该怎么做 阅读完问题后,若你们认为问题的标题是错误的,你们应该改变标题 我正在使用console.log进行调试,但如果浏览器IE.I为此问题执行以下功能,则会导致错误如何在javascript中代理函数?,javascript,oop,Javascript,Oop,我是javascript的新手。所以我不知道我要找的名字是什么,我该怎么做 阅读完问题后,若你们认为问题的标题是错误的,你们应该改变标题 我正在使用console.log进行调试,但如果浏览器IE.I为此问题执行以下功能,则会导致错误 var mylog=function(){ if (devmode && window.console){ console.log(arguments); } }; mylog("debugging"); 现在我想使用所
var mylog=function(){
if (devmode && window.console){
console.log(arguments);
}
};
mylog("debugging");
现在我想使用所有的控制台功能,没有错误,我可以这样做如下
var myconsole={
log:function(){
if (devmode && window.console){
console.log(arguments);
}
}
,error:function(){
if (devmode && window.console){
console.error(arguments);
}
}
...
...
...
};
但我不想将所有控制台函数分别添加到myconsole对象中。
我可以用下面的代码在PHP中实现这一点
class MyConsole
{
function __call($func,$args)
{
if ($devmode && function_exists('Console')){
Console::$func($args); // Suppose that there is Console class.
}
}
}
MyConsole::warn("name",$name);
MyConsole::error("lastname",$lastname);
这可以通过\uuuu noSuchMethod\uuuu
方法实现,但这只适用于firefox
感谢您的帮助。不幸的是,您无法在JavaScript中实现这一点,该语言不支持“无此方法”的概念 您有两个选择: 备选案文1: 使用字符串作为方法名称,例如:
function myconsole(method) {
var args;
if (devmode && window.console) {
args = Array.prototype.slice.apply(arguments, 1);
window.console[method].apply(window.console, args);
}
}
用法:
myconsole("log", "message");
myconsole("error", "errormessage");
myconsole
的主要内容如下:
args = Array.prototype.slice.apply(arguments, 1);
window.console[method].apply(window.console, args);
第一行复制提供给myconsole
的所有参数,但第一行除外(它是我们要使用的方法的名称)。第二行从控制台
对象中检索由方法
中的字符串命名的属性的函数对象,然后通过JavaScript调用它,为它提供这些参数
备选案文2:
我想到了第二种选择,它最好直接用代码表示:
var myconsole = (function() {
var methods = "log debug info warn error assert clear dir dirxml trace group groupCollapsed groupEnd time timeEnd profile profileEnd count exception table".split(' '),
index,
myconsole = {},
realconsole = window.console;
for (index = 0; index < methods.length; ++index) {
proxy(methods[index]);
}
function proxy(method) {
if (!devmode || !realconsole || typeof realconsole[method] !== 'function') {
myconsole[method] = noop;
}
else {
myconsole[method] = function() {
return realconsole[method].apply(realconsole, arguments);
};
}
}
function noop() {
}
return myconsole;
})();
var myconsole=(函数(){
var methods=“log debug info warn error assert clear dir dirxml跟踪组groupCollapsed groupEnd time timeEnd profile profileEnd count exception table”。拆分(“”),
指数
myconsole={},
realconsole=window.console;
对于(索引=0;索引
然后,您只需在
myconsole
上正常调用log
、warn
等。不幸的是,在JavaScript中无法做到这一点,该语言不支持“无此方法”的概念
您有两个选择:
备选案文1:
使用字符串作为方法名称,例如:
function myconsole(method) {
var args;
if (devmode && window.console) {
args = Array.prototype.slice.apply(arguments, 1);
window.console[method].apply(window.console, args);
}
}
用法:
myconsole("log", "message");
myconsole("error", "errormessage");
myconsole
的主要内容如下:
args = Array.prototype.slice.apply(arguments, 1);
window.console[method].apply(window.console, args);
第一行复制提供给myconsole
的所有参数,但第一行除外(它是我们要使用的方法的名称)。第二行从控制台
对象中检索由方法
中的字符串命名的属性的函数对象,然后通过JavaScript调用它,为它提供这些参数
备选案文2:
我想到了第二种选择,它最好直接用代码表示:
var myconsole = (function() {
var methods = "log debug info warn error assert clear dir dirxml trace group groupCollapsed groupEnd time timeEnd profile profileEnd count exception table".split(' '),
index,
myconsole = {},
realconsole = window.console;
for (index = 0; index < methods.length; ++index) {
proxy(methods[index]);
}
function proxy(method) {
if (!devmode || !realconsole || typeof realconsole[method] !== 'function') {
myconsole[method] = noop;
}
else {
myconsole[method] = function() {
return realconsole[method].apply(realconsole, arguments);
};
}
}
function noop() {
}
return myconsole;
})();
var myconsole=(函数(){
var methods=“log debug info warn error assert clear dir dirxml跟踪组groupCollapsed groupEnd time timeEnd profile profileEnd count exception table”。拆分(“”),
指数
myconsole={},
realconsole=window.console;
对于(索引=0;索引
然后你只需像往常一样在myconsole上调用
log
、warn
等。谢谢@T.J.Crowder。你的建议很好。有不同的方法可以做到这一点。@mesuutt:我想出了第二个主意,我已经添加了这个主意。将来,ECMAScript6(当它发生时)可能会通过一个称为“代理”的功能避免出现这样的名称列表。但它还没有很好的定义,当然ECMAScript 6还有一段时间,等待旧浏览器消亡的时间甚至更长……我一直在寻找你的第二个替代函数:)Thanks@mesuutt:很好,很高兴这有帮助!谢谢@T.J.Crowder。你的建议很好。有不同的方法可以做到这一点吗?@mesuutt:我想出了第二个主意,我已经补充了。将来,ECMAScript6(当它发生时)可能会通过一个称为“代理”的功能避免出现这样的名称列表。但它还没有很好的定义,当然ECMAScript 6还有一段时间,等待旧浏览器消亡的时间甚至更长……我一直在寻找你的第二个替代函数:)Thanks@mesuutt:很好,很高兴这有帮助!