在所有浏览器上调用未来的javascript函数兼容性?

时间:2010-02-27 16:54:03

标签: javascript jquery

假设我的功能如下

function doSomethingNow(){

  callSomethingInFutureNotExistNow();
}

目前doSometingNow()创建时callSomethingInFutureNotExistNow()尚不存在。它将在未来创建,在firefox上,这不会在firebug上显示任何错误。这些功能是否会兼容所有浏览器而不会出错?

1 个答案:

答案 0 :(得分:1)

由于javascript未编译,假设您在声明doSomethingNow()之前未致电callSomethingInFutureNotExistNow,则不应该收到您发布的代码的任何错误。

为安全起见,您可能需要进行一些空检查

function doSomethingNow(){ 

  if (callSomethingInFutureNotExistNow) {
    callSomethingInFutureNotExistNow(); 
  }
} 

或者如果你想要更严格,你可以像这样进行类型检查

function doSomethingNow(){ 

  if (typeof(callSomethingInFutureNotExistNow) === 'function') {
    callSomethingInFutureNotExistNow(); 
  }
}