简体   繁体   English

为什么Javascript内部/外部文件中的错误也会停止以下代码?

[英]why is error in Javascript internal/external file stops the below code too?

The Error in JavaScript internal/external file also stops the below code JavaScript内部/外部文件中的错误也会停止以下代码

For example: 例如:

var myObj = {};
myObj.other.getName = function(){
  console.log('other is not defined');
};
alert('this will not show');

in above code, the alert will not come as the above code has error. 在上面的代码中,由于上面的代码有错误,因此不会发出警报。

I added the same code in one file tst1.js and below this add one more file tst2.js. 我在一个文件tst1.js添加了相同的代码,在此之下添加了另一个文件tst2.js. put alert('in tst2.js') in it. 放入alert('in tst2.js') the tst2 alert come while tst1 not. tst2警报来了,而tst1没有。 it is some what related to code compilation/interpretation. 它与代码编译/解释有关。

It's much appreciated If someone explain me this behavior :) 如果有人向我解释此行为,我将不胜感激:)

This is the default behaviour of JavaScript. 这是JavaScript的默认行为。 Avoid errors and the code will run normally. 避免错误,代码将正常运行。 Also you can handle errors with try...catch : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch 您也可以使用try...catch处理错误: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

All JS implementations are, AFAIK, single threaded. 所有的JS实现都是AFAIK,是单线程的。 This means that all of your code is executed sequentially, on a single thread (logically). 这意味着所有代码都在一个线程上(逻辑上)按顺序执行。 If this thread encounters a fatal error, it grinds to a halt. 如果该线程遇到致命错误,它将停止运行。 All code that comes after the point where the error occurs is ignored (the JS engine halted, no work is done anymore). 错误发生之后的所有代码都将被忽略(JS引擎停止,不再进行任何工作)。
To clarify: it does not matter how many files you have. 需要说明的是:拥有多少文件并不重要。 All of the JS code is stringed together into one big script, and this one script is executed sequentially (execution point starts at line 1 of the first script, and ends at the last line of the last script). 所有的JS代码都串在一起形成一个大脚本,然后依次执行一个脚本(执行点从第一个脚本的第1行开始,到最后一个脚本的最后一行结束)。 Any errors in that code will cause the overall execution to grind to a halt: 该代码中的任何错误都将导致整体执行停止:

//file1.js
var foo = (function()
{
    console.log('This file is flawless, but pointless');
}());
//file2.js
foo();//calls previously defined function, assigned to var foo
//file3.js
fudhfsiufhi;//ERROR
//file4.js
foo();//will never get executed, because an error occurred in file3.js

Remove file3, or indeed fix the error, and everything will work as expected. 删除file3,或者确实修复错误,一切都会按预期进行。
Allthough JS code is executed/evaluated sequentially, event handlers, callbacks, intervals, and timeouts might lead you to believe otherwise. 尽管JS代码是按顺序执行/评估的,但是事件处理程序,回调,间隔和超时可能会让您相信其他情况。 Couple that with the fact that you have some control over what code is executed when, but not full control , and you get situations that, at first, seem rather counter intuitive. 再加上您对什么时候执行什么代码有一定的控制权,但又没有完全的控制权 ,您会遇到一开始看上去很不直观的情况。 Consider this: 考虑一下:

setTimeout(function()
{
    massive(syntaxError) -123 + "here";
},0);//zero MS
alert('This will show');

This oddity is well documented, but it has to do with JS having a callback/handler loop, and queue. 这种奇怪的现象已经有充分的文档证明,但是与具有回调/处理程序循环和队列的JS有关。 The setTimeout sets a timeout, to call the anonymous function in 0ms (immediately), but that callback is sent to the queue, which is checked periodically. setTimeout设置一个超时,以在0ms(立即)中调用匿名函数,但是该回调将发送到队列,并定期进行检查。 Before the queue is checked (and the callback invoked), the alert will show. 在检查队列(和调用回调)之前,将显示警报。 That's why the interval you pass to setTimeout or setInterval is not guaranteed to be exactly N milliseconds. 这就是为什么不能保证您传递给setTimeoutsetInterval的间隔恰好是 N毫秒。
You can postpone a call to a method somewhat, by adding a call to the queue, like in the snippet above. 您可以通过将调用添加到队列中来稍微推迟对方法的调用,如上面的代码段所示。 But when the queue is processed, and what order the queued calls will be performed in are things you have no real say in. No say whatsoever. 但是,当队列被处理后,排队的呼叫将以什么顺序执行,这是您无话可说的事情。

It doesn't matter how many files, or how many statements that come before or after the problematic piece of code: there is no thread left to carry on. 无论有多少文件,或在有问题的代码段之前或之后出现多少条语句,都没有关系:没有线程可以继续进行。

The code you posted has a pretty clear error in it: you're assigning a property to other (a property of myObj , but this property is not defined anywhere, let alone defined as an object. Fix it by declaring properties first, before accessing them: 您发布的代码中有一个非常明显的错误:您正在将一个属性分配给other属性( myObj的属性,但是此属性未在任何地方定义,更不用说定义为对象了。请在访问之前先声明属性来解决此问题他们:

var myObj = {other: {}};//other is an empty object literal
myObj.other.getName = function()
{
    console.log('This works');
};
alert('And the alert will show');

Your current code evalutes to: 您当前的代码评估为:

var myObj = {};//object
var (myObj.other).getName = ...;
     //evaluates to undefined
    undefined.getName = ...//ERROR

undefined is a primitive value, actually signifying the absence of a value. undefined是原始值,实际上表示没有值。 undefined , therefore, cannot be accessed as an object, it can't be assigned properties. 因此, undefined不能作为对象访问,也不能分配属性。

Note: 注意:
This is just for completeness' sake, but JS is indeed single-threaded most of the time, but ECMAScript5 introduced Worker 's which allow for some restricted form of multi-threading (without shared state, for example). 这只是出于完整性的考虑,但是JS实际上大多数时候确实是单线程的,但是ECMAScript5引入了Worker ,它允许某种受限形式的多线程(例如,没有共享状态)。 Read through the MDN documentation on workers if you want to know more. 如果您想了解更多信息,请通读有关工人的MDN文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM