gpt4 book ai didi

javascript - 面向对象的 JavaScript 转换器

转载 作者:行者123 更新时间:2023-11-28 11:34:26 24 4
gpt4 key购买 nike

所以我正在使用由 JavaScript 驱动的转换器应用程序,现在我正在尝试使用所有措施创建一个巨大的对象。但每当我调试它时,它都会说这样的话:

Expected ';' and instead saw '.' (var units.length = {};)
Expected '.' at column 1, not column 10 (var units.length = {};)
Unexpected '.' (var units.length = {};)
etc.

自从我用 JS 编码以来已经很长时间了,所以我对此有些困惑,希望得到任何帮助,代码如下:

var units = {};
var units.length = {};
var units.time = {};
var units.mass = {};
var units.temperature = {};

//Starting with Length
units.length.meter = 1;
units.length.meters = 1;

units.length.inch = 0.0254;
units.length.inches = 0.0254;

units.length.foot = 0.3048;
units.length.feet = 0.3048;

units.length.yard = 0.9144;
units.length.yards = 0.9144;

units.length.mile = 1609.344;
units.length.miles = 1609.344;

...

最佳答案

仅使用var声明变量,而不是创建现有对象的属性:

var units = {};
units.length = {};
units.time = {};
units.mass = {};
units.temperature = {};

//Starting with Length
units.length.meter = 1;
units.length.meters = 1;

units.length.inch = 0.0254;
units.length.inches = 0.0254;

units.length.foot = 0.3048;
units.length.feet = 0.3048;

units.length.yard = 0.9144;
units.length.yards = 0.9144;

units.length.mile = 1609.344;
units.length.miles = 1609.344;

同时考虑

var units = {
length: {
meter: 1,
meters: 1,
inch: 0.0254,
inches: 0.0254,
foot: 0.3048,
feet: 0.3048,
yard: 0.9144,
yards: 0.9144,
mile: 1609.344,
miles: 1609.344
},
time: {},
mass: {},
temperature: {}
};

关于javascript - 面向对象的 JavaScript 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28254748/

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