gpt4 book ai didi

javascript - JSLint 对象错误

转载 作者:行者123 更新时间:2023-12-01 03:57:17 26 4
gpt4 key购买 nike

我正在使用 JSLint 来检查以下代码:

'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);
}

当我尝试检查此代码时,我的对象中的每个属性都会收到一条错误消息,表明该属性未定义。但是,我没想到必须定义对象属性?预先感谢您的帮助!

最佳答案

将对象移到函数后面,例如

"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
};

您会收到一些警告,但不会出现错误。

关于javascript - JSLint 对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com