在Javascript中伪造函数重载的最佳方法是什么?
我知道不可能像在其他语言中一样重载Javascript中的函数.如果我需要一个函数有两个用途foo(x)和foo(x,y,z)这是最好的/优选的方法:
y = y || 'default'新手问题:我有一个包含大量字符串的"开关".按字母顺序分割是否有速度优势,就像这样?
switch(myString.substring(0,1)){
case "a" : switch(myString){
case "a string beginning with a" : runCode(); break;
case "another string beginning with a" : runCode(); break;
} break;
case "b" : switch(myString){
case "by golly another string" : runCode(); break;
case "blimey - hundreds of strings" : runCode(); break;
//... etc
Run Code Online (Sandbox Code Playgroud)
或者,无论如何,脚本语言都会读取每一行,只是为了找到封闭的括号?