【问题标题】:JSLint Object ErrorJSLint 对象错误
【发布时间】:2017-03-01 16:35:29
【问题描述】:

我正在使用 JSLint 对以下代码进行 lint:

'use strict';

var mathService = {
   add: add,
   subtract: subtract,
   multiply: multiply,
   divide: divide,
   power: power,
   squareRoot: squareRoot
};

function add(first, second) {
   return first + second;
}

function subtract(first, second) {
   return first - second;
}

function multiply(first, second) {
   return first * second;
}

function divide(first, second) {
   return first / second;
}

function power(first, second) {
   return Math.pow(first, second);
}

function squareRoot(first) {
   return Math.sqrt(first);
}

当我尝试 lint 这段代码时,我会收到我的对象中每个属性的错误消息,表明它是未定义的。但是,我不认为必须定义对象属性?提前感谢您的帮助!

【问题讨论】:

  • 确切的错误信息是什么?我假设它抱怨函数在定义之前就被分配了。
  • 请注意,错误是指对象定义中冒号 right 的项目 - 属性名称本身很好。如果您更改属性或函数名称以使它们不同,则可以看到这一点 - 错误指的是右侧。
  • 感谢大家的帮助!幸运的是,Clyde Lobo 的代码成功了。

标签: javascript jslint


【解决方案1】:

在函数之后移动对象,比如

"use strict";

function add(first, second) {
   return first + second;
}

function subtract(first, second) {
   return first - second;
}

function multiply(first, second) {
   return first * second;
}

function divide(first, second) {
   return first / second;
}

function power(first, second) {
   return Math.pow(first, second);
}

function squareRoot(first) {
   return Math.sqrt(first);
}

var mathService = {
   add: add,
   subtract: subtract,
   multiply: multiply,
   divide: divide,
   power: power,
   squareRoot: squareRoot
};

您会收到一些警告,但没有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2013-01-20
    • 2012-02-29
    • 2012-07-14
    • 2011-08-17
    • 2014-11-30
    相关资源
    最近更新 更多