繁体   English   中英

JavaScript中的严格模式限制

[英]Strict mode constrictions in Javascript

嘿,所以我有一个函数,可以从输入框中获取字符串,并将其拆分为数字和字母,如下所示:

function sepNsLs() {
  "use strict"; 
  var letterArray = [];
  var numberArray = [];
  separatorSpacerator();
  var L = 0;
  var listResult = document.getElementById("listInput").value;
  var splitResult = listResult.split(separator.sep);
  for (; L < splitResult.length; L++) {
    if (isNaN(splitResult[L])) {
      letterArray.push(splitResult[L]);
    } else if (Number(splitResult[L])) {
      numberArray.push(splitResult[L]);
    }
  }
}

我的程序必须完美地通过JSLint,这意味着我需要在严格模式下使用函数。 我现在仅将它们置于严格模式下,这意味着我以后尝试调用我在SepNsLs函数中声明并填充的letterArray和numberArray的函数不再调用那些数组,并且这些数组未声明。 这是其中之一的代码:

function addNumbers() {
  "use strict"; 
  var sum = 0;
  var i = 0;
  sepNsLs();
  while (i < numberArray.length) {
    sum = sum + Number(numberArray[i]);
    i++;
  }

如您所见,我在addNumbers函数中调用sepNsLs函数,但是由于严格的模式,我无法使用sepNsLs创建的数组。 我该如何解决? 此外,是否有像javascript beautifier这样的网站可以修复我当前的代码以符合严格的模式约定?

编辑:分隔符在此处声明为全局变量:

var separator = {
  sep: 0
};

splitterSpacerator可以这样,如果我选择在一个空格处分割输入字符串,则告诉我的程序在空格处分割的输入框会声明“ Space”一词,因此我可以看到它是一个空格,我正在将字符串分割为。

function separatorSpacerator() {
  "use strict"; 
  var list = document.getElementById("listInput").value;
  if (document.getElementById("separatorInput").value === "space") {
    separator.sep = " ";
  } else if (document.getElementById("separatorInput").value === " ") {
    separator.sep = " ";
    document.getElementById("separatorInput").value = "space";
  } else {
    separator.sep = document.getElementById("separatorInput").value;
  }
  if (list.indexOf(separator.sep) === -1) {
    alert("Separator not found in list!");
    clearResults();
  }
}

您的问题是范围之一。 当您尝试在addNumbers内部访问numberArray ,该numberArray不存在。

您有两种选择:

  1. 全局设置每个函数中需要访问的所有变量。
  2. 将所有函数包装在外部函数中,然后将“全局”变量放入该外部范围。

更好的选择是#2,因为实际上不会用变量污染全局范围。 您可以在外部函数的顶部声明"use strict" ,它将强制其中的所有内容进入严格模式。

像这样:

(function() {
    "use strict";

    // These are now in-scope for all the inner functions, unless redclared
    var letterArray = [], numberArray = [], separator = {sep: 0};

    function sepNsLs() {
       // code goes here
    }
    function addNumbers(){ 
       // code goes here
    }
    function separatorSpacerator(){ 
       //code goes here
    }
    // ...more functions and stuff

    // and then call...
    theFunctionThatKicksOffTheWholeProgram();

}());

我不能使用sepNsLs创建的数组。 我该如何解决?

解决此问题的一种方法是返回sepNsLs用例如元组创建的数组-return return [numberArray, letterArray]; ,然后像这样使用它:

a)es6语法:

var [numberArray, letterArray] = sepNsLs();

b)ES6之前的语法:

var split = sepNsLs(),
    numberArray = split[0],
    letterArray = split[1];

addNumbers功能也应该可能返回sum -否则,因为它代表它不产生任何有意义的结果。

尽管与该问题无关,并且更多地是关于约定惯用命名的问题-您可能希望探讨匈牙利表示法及其缺点。

变量letterArraynumberArray声明为函数sepNsLs局部变量,仅在该范围内访问(无论是否严格模式)。 这是一个例子:

 function foo() { var fooVar = 5; console.log(fooVar); }// fooVar get destroyed here function bar() { console.log(fooVar); // undefined because fooVar is not defined } foo(); bar(); 

范围通常是从大括号{到匹配的大括号} 范围内声明的任何事物仅与该范围一起使用。 范例2:

 var globalVar = 5; // belongs to the global scope function foo() { var fooVar = 6; // belongs to the foo scope function bar1() { console.log(globalVar); // will look if there is a globalVar inside the scope of bar1, if not it will look if there is globalVar in the upper scope (foo's scope), if not it will in the global scope. console.log(fooVar); // same here var bar1Var = 7; // belongs to bar1 scope } function bar2() { var globalVar = 9; // belongs to the bar2 scope (shadows the global globalVar but not overriding it) console.log(globalVar); // will look if there is a globalVar in this scope, if not it will look in one-up scope (foo's scope) .... untill the global scope console.log(bar1Var); // undefined because bar1Var doesn't belong to this scope, neither to foo's scope nor to the global scope } bar1(); bar2(); console.log(globalVar); // prints 5 not 9 because the serach will be in foo's and the global scope in that order } foo(); 

您需要做的是对变量letterArraynumberArray ,以使sepNsLsaddNumbers (在它们两者sepNsLs一个作用域)都可以访问它们。 或从sepNsLs返回值并将其存储在addNumbers内的变量中。 像这样:

function sepNsLs() {

  // logic here

  return numberArray; // return the array to be used inside addNumbers
}

function addNumbers() {

  var arr = sepNsLs(); // store the returned value inside arr

  for(var i = 0; i < arr.length; ... // work with arr

}

暂无
暂无

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

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