简体   繁体   English

javascript中的变量使用和不使用'var,let'时的不同结果

[英]Different results when using and when not using 'var, let' for variables in javascript

I'm going to sort an array using merge sort.我将使用归并排序对数组进行排序。 I built a function for it like this and executed it using Node.js.我为它构建了一个这样的函数并使用 Node.js 执行它。

 arr = [57, 23, 45, 12, 49, 59]; //sort array using merge sort function mergeSort(arr) { //nested function to merge decomposed arrays function mergeArrays(leftArr, rightArr) { sortedArr = []; while (leftArr.length && rightArr.length) { if (leftArr[0] > rightArr[0]) { sortedArr.push(rightArr.shift()); } else { sortedArr.push(leftArr.shift()); } } sortedArr.push(...leftArr, ...rightArr); return sortedArr; } //end line of the nested array if (arr.length > 1) { m = Math.floor(arr.length / 2); // console.log(arr.splice(m)) leftArr = mergeSort(arr.splice(0, m));//this line makes the defect if there is no "var or let" in front of it. rightArr = mergeSort(arr); return mergeArrays(leftArr, rightArr); } else { return arr; } } console.log(mergeSort(arr));

Above code is supposed to sort the initial array of the code.上面的代码应该对代码的初始数组进行排序。

arr = [57, 23, 45, 12, 49, 59]//this array

So it is supposed to give a result like this.所以它应该给出这样的结果。

[12, 23, 45, 49, 57, 59]

Instead it gives me a result like this.相反,它给了我这样的结果。

[49, 59]

But when I put "var" or "let" in front of "leftArr" variable (please look inside the bottom "if" condition, I have mentioned the relevant line by commenting on it) the code gives the correct result.但是当我将“var”或“let”放在“leftArr”变量前面时(请查看底部的“if”条件,我通过评论提到了相关行)代码给出了正确的结果。 I know we have to declare variables using var, let or const.我知道我们必须使用 var、let 或 const 来声明变量。 But even though I didn't do it javascript had never gave me any errors until now.但即使我没有这样做,javascript 直到现在才给我任何错误。 So someone please help me to understand why this defect is occurring when "var" is not presented.所以请有人帮我理解为什么在没有出现“var”时会出现这个缺陷。 Thanks in advance to anyone who would like to help me.提前感谢任何愿意帮助我的人。

From var on MDN :来自MDN 上的var

Unqualified identifier assignments不合格的标识符分配

The global object sits at the top of the scope chain.全局对象位于作用域链的顶部。 When attempting to resolve a name to a value, the scope chain is searched.尝试将名称解析为值时,将搜索范围链。 This means that properties on the global object are conveniently visible from every scope, without having to qualify the names with globalThis.这意味着全局对象上的属性在每个范围内都可以方便地看到,而不必使用globalThis. or window.window. or global.global. . .

Because the global object has a String property ( Object.hasOwn(globalThis, 'String') ), you can use the following code:因为全局对象有一个String属性( Object.hasOwn(globalThis, 'String') ),你可以使用下面的代码:

 function foo() { String('s') // Note the function `String` is implicitly visible }

So the global object will ultimately be searched for unqualified identifiers.所以最终将在全局对象中搜索不合格的标识符。 You don't have to type globalThis.String , you can just type the unqualified String .您不必键入globalThis.String ,您只需键入不合格的String The corollary, in non-strict mode, is that assignment to unqualified identifiers will, if there is no variable of the same name declared in the scope chain, assume you want to create a property with that name on the global object.在非严格模式下的推论是,如果在作用域链中没有声明同名的变量,则对非限定标识符的赋值将假定您要在全局对象上创建一个具有该名称的属性。

 foo = 'f' // In non-strict mode, assumes you want to create a property named `foo` on the global object Object.hasOwn(globalThis, 'foo') // true

In ECMAScript 5, this behavior was changed for strict mode .在 ECMAScript 5 中,此行为已针对严格模式进行了更改。 Assignment to an unqualified identifier in strict mode will result in a ReferenceError , to avoid the accidental creation of properties on the global object.在严格模式下分配给非限定标识符将导致ReferenceError ,以避免在全局对象上意外创建属性。

Note that the implication of the above, is that, contrary to popular misinformation, JavaScript does not have implicit or undeclared variables, it merely has a syntax that looks like it does.请注意,上面的含义是,与流行的错误信息相反,JavaScript 没有隐式或未声明的变量,它只是有一个看起来像它的语法。

Read more on var , let , and const阅读有关varletconst的更多信息

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

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