javascript对象中的任意方法名称

时间:2012-03-14 21:43:07

标签: javascript function object arguments

通过使用我想要的一些额外功能滚动我自己的console.log变体,我正在学习更多关于javascript OOP的知识。

到目前为止我已经

debug = {
        consoleAvailable :  (typeof console == "object"),
        reportToConsole : 0,
        list            : [],
        setReporting    : function(level){
                            this.reportToConsole = level;
                            return true;
                        },
        log             : function(){
                            if (this.reportToConsole>0 && this.consoleAvailable && typeof console.log=="function")  console.log.apply(console, this.log.arguments);
                            this.list.push({'type':'log', 'msg':this.log.arguments});
                            return true;
                        },
};

这一切都运行良好,但我不想列出所有日志,错误,警告等功能。相反,我希望能够只输入debug。[something]和函数来解释[something]并以与日志函数相同的方式执行它。

这甚至可能吗?如果是这样我该怎么办呢?

以下是我希望能够做的一些例子。

debug.setReporting(1); //yes, I want to print to console

debug.log('foo', 'bar', 'baz'); //arbitrary number of arguments (this is already working)
debug.error('qux'); //function I haven't named, but there is a console.error function
debug.arbitraryName([1, 2, 3]); //no function for console.arbitraryName (ideally it would just console.log the argument(s)

修改

好吧,看起来@Rob W的方法是可行的,但是我在实施方面遇到了麻烦。似乎我没有正确或类似地传递函数的名称。我在这里有一个小提示,显示问题http://jsfiddle.net/xiphiaz/mxF4m/

结论

看起来有太多的浏览器怪癖要在没有编写浏览器特定代码的情况下获得真正的通用调试器,所以我刚刚列出了我最常用的日志功能(日志,警告和错误)。这确实让我可以选择进一步自定义每个函数的结果。

结果:

debug = {
    consoleAvailable :  (typeof console == "object"),
    reportToConsole : 0,
    list            : [],
    setReporting    : function(level){
                        this.reportToConsole = level;
                        return true;
                    },
    log             : function(){
                        if (this.reportToConsole>0 && this.consoleAvailable && typeof console.log=="function")  console.log.apply(console, this.log.arguments);
                        this.list.push({type:'log', msg:this.log.arguments});
                        return true;
                    },
    warn            : function(){
                        if (this.reportToConsole>0 && this.consoleAvailable && typeof console.warn=="function") console.warn.apply(console, this.warn.arguments);
                        this.list.push({type:'warn', msg:this.warn.arguments});
                        return true;
                    },
    error           : function(){
                        if (this.reportToConsole>0 && this.consoleAvailable && typeof console.error=="function")    console.error.apply(console, this.error.arguments);
                        this.list.push({type:'error', msg:this.error.arguments});
                        return true;
                    }
};

debug.setReporting(1);

debug.log('foo', 'bar', 'baz');
debug.error('qux');
debug.warn({test:"warning"});
console.log(debug.list);

1 个答案:

答案 0 :(得分:1)

您可以使用Object.getOwnProprtyNames方法获取所有属性的数组(包括不可枚举的属性)。然后,枚举它,并检查console[key]是否是一个函数。如果是,请使用该方法扩展您自己的对象。

Object.getOwnPropertyNames(console)

关于你的最后一个问题,有一个非标准__noSuchMethod__方法拦截对未定义函数的调用。

我强烈建议使用我的第一个方法,因为console方法不会神奇地变大。它还使调试更容易。