JS未捕获引用错误,... [英] JS uncaught reference error, ...

查看:143
本文介绍了JS未捕获引用错误,...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一个带有源代码的HTML,来自 [ ^ ]。它工作正常。

然后我将组件加载到另一个开发页面。加载JS引用后,页面会在调试时显示多个错误。一些错误是:

未捕获的ReferenceError:未定义Cookie

未捕获TypeError:Element.extend不是函数

是否到期JS引用之间的冲突?谢谢,如果你能提供帮助。

解决方案

JavaScript引用之间没有这样的冲突。您只是尝试取消引用 undefined 对象。这是一个特定于JavaScript的特殊对象,与 null 和所有其他值不同。当您声明某个变量但不初始化它时,运行时系统会使用它。此外,您可以随时将 undefined 分配给任何其他对象。



考虑一下:

  var  Cookie;  //   undefined  
Cookie = undefined ; // 与上述相同

function A(){alert( 做点什么); }
Cookie = A(); // A未返回任何内容,
// 所以Cookie仍未定义

// < span class =code-comment>现在,考虑一下:

var someOtherVariable = Cookie; // 罚款!

// 但是如果
抛出异常 var someOtherVariable = Cookie.a;



我在Mozilla和Chrome上试过它,它实际上抛出了 TypeError 这个错误信息,但我可以想象其他一些浏览器抛出不同的异常对象。参考错误来自不同的情况,当某个变量(下面我的例子中的 f )甚至没有声明时:

  var  someOtherVariable = f; 
// ReferenceError:f未定义



未定义对象(原始值)上,请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined [<一个href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefinedtarget =_ blanktitle =New Window> ^ ],

不要与 null混淆https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null [ ^ ]。



您描述的剩余异常是不是函数。考虑这个例子:

  var 元素= {};  //  值空对象 
Element.extend = 42 ; // fine

Element.extend();
// TypeError:
// Element.extend不是函数

// 另一种可能性是:
Element.somethingNotCreated();
// TypeError:
// Element.somethingNotCreated不是函数



我希望异常消息是自我解释。



请不要问我你的代码做错了什么;我不知道,坦白说,不想知道。你只需要了解第一原则,而不是挖掘它。



-SA


< blockquote>嗯,你真的应该从你下载代码的地方发布这个问题。

因为我们真的不知道JS中实际写了什么。



希望我很清楚:)



-KR


I tested a HTML w/ the loading of the source code from [^]. It works fine.
Then I loaded the components into another developed page. Once I loaded the JS references, the page displays multiple errors in debugging. Some of the errors are:
Uncaught ReferenceError: Cookie is not defined
Uncaught TypeError: Element.extend is not a function
Is it due to the conflicts between JS references? Thanks if you can help.

解决方案

There are no such conflicts between JavaScript references. You are just trying to dereference undefined object. This is a special object specific to JavaScript, distinct from null and all other values. It is used by the runtime system when you declare some variable but don't initialize it. Also, you can assign undefined to any other object at any time.

Consider this:

var Cookie; // undefined
Cookie = undefined; // same as above

function A() { alert("Do something"); }
Cookie = A(); // A did not return anything,
// so Cookie is still undefined

// Now, consider this:
var someOtherVariable = Cookie; // fine!

// But exception if thrown if 
var someOtherVariable = Cookie.a;


I tried it on Mozilla and Chrome, it actually throws TypeError with this error message, but I can imagine that some other browser throws different exception object. Reference errors come in different situations, when some variable (f in my example below) is not even declared:

var someOtherVariable = f;
// ReferenceError: f is not defined


On undefined object (primitive value), please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined[^],
not to be confused with null: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null[^].

Remaining exception you described is "not a function". Consider this example:

var Element = { }; //value empty object
Element.extend = 42; //fine

Element.extend();
// TypeError:
// Element.extend is not a function

// Another possibility is this:
Element.somethingNotCreated();
// TypeError:
//Element.somethingNotCreated is not a function


I hope the exception message is self-explained.

Please don't ask me what your code did wrong; I don't know and, frankly, don't want to know. You just need to understand the first principles, than you can dig it out.

—SA


Umm, you really should post this question from where you downloaded the code.
Because we really don't know what actually has written in that JS.

Hope I'm clear :)

-KR


这篇关于JS未捕获引用错误,...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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