JS未捕获引用错误,... [英] JS uncaught reference error, ...
问题描述
我测试了一个带有源代码的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 dereferenceundefined
object. This is a special object specific to JavaScript, distinct fromnull
and all other values. It is used by the runtime system when you declare some variable but don't initialize it. Also, you can assignundefined
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 throwsTypeError
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
Onundefined
object (primitive value), please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined[^],
not to be confused withnull
: 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屋!