面向对象 JavaScript 转换器
Object-Oriented JavaScript Converter
所以我在一个由 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: {}
};
属性前没有var
,只有变量。
var units = {
length: {},
time: {},
mass: {},
temperature : {}
};
注意:长度保留为array/string长度,你应该避免这样命名一个属性。并且您应该使用扩展方法来避免重复 units
和 units.length
.
var units = {
length: {
meter: 1,
meters: 1,
inch: 0.0254,
inches: 0.0254 // ...
},
time: {},
mass: {},
temperature : {}
};
所以我在一个由 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: {}
};
属性前没有var
,只有变量。
var units = {
length: {},
time: {},
mass: {},
temperature : {}
};
注意:长度保留为array/string长度,你应该避免这样命名一个属性。并且您应该使用扩展方法来避免重复 units
和 units.length
.
var units = {
length: {
meter: 1,
meters: 1,
inch: 0.0254,
inches: 0.0254 // ...
},
time: {},
mass: {},
temperature : {}
};