ESLint - JavaScript中的 "使用严格 "有什么作用,背后的原因是什么?

JSHint / javascript / syntax / jslint / use-strict

最近,我通过Crockford的JSLint运行了一些JavaScript代码,并给出了以下错误:

Mark Rogers



Answer #1

防止全局变量声明:

var tree1Data = { name: 'Banana Tree',age: 100,leafCount: 100000};

function Tree(typeOfTree) {
    var age;
    var leafCount;

    age = typeOfTree.age;
    leafCount = typeOfTree.leafCount;
    nameoftree = typeOfTree.name;
};

var tree1 = new Tree(tree1Data);
console.log(window);

未捕获的语法错误。在严格模式下,不允许在对象文字中出现重复的数据属性。

"use strict";
var tree1Data = {
    name: 'Banana Tree',
    age: 100,
    leafCount: 100000,
    name:'Banana Tree'
};