JavaScript功能未在Firefox中定义? [英] JavaScript function not defined in Firefox?

查看:75
本文介绍了JavaScript功能未在Firefox中定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试我的Firefox网站的下一个层次,在JavaScript中发现了一个非常奇怪的错误。在Firefox中,这些函数是否需要在引用这些函数的任何代码行之上进行定义?这对我来说似乎很奇怪。



这是导致错误的简化版本:



  var myClass = new MyClass(myCallback); 

函数myCallback(){
//代码
}

它抛出了以下错误:错误:myCallback未定义



这是我需要的做它在Firefox工作:



  var myCallback = function(){
// code
}

var myClass = new MyClass(myCallback);

所以我的问题是:这是正常的行为还是别的事情?我的意思是,在将来编写代码时,是否需要考虑这个问题?

解决方案

在调用它之前必须定义函数当您以格式启动时:

  myCallback = function(){
// code
}

但是,当您以格式启动时,可以定义任何位置:

  function myCallback(){
// code
}


I'm currently debugging the next tier of my website in Firefox and found a really weird bug in my JavaScript. Is it true, in Firefox, that functions need to be defined above any lines of code referencing those functions? That seems really strange to me.

Here's a simplified version of what was causing bugs:

var myClass = new MyClass(myCallback);

function myCallback() {
    // code
}

It threw the following bug: Error: myCallback is not defined

And here's what i needed to do for it to work in Firefox:

var myCallback = function() {
    // code
}

var myClass = new MyClass(myCallback);

So my question is: Is this normal behavior or was something else going on? I mean, do I need to take this into consideration when coding in the future?

解决方案

You must define the function before calling it when you initiate with the format:

myCallback = function() {
    // code
}

But it should be ok to define anywhere when you initiate with the format:

function myCallback() {
    // code
}

这篇关于JavaScript功能未在Firefox中定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆