简体   繁体   English

面向对象的JavaScript转换器

[英]Object-Oriented JavaScript Converter

So I am working in a converter application powered by JavaScript, and right now I am trying to create a huge object with all the measures. 因此,我正在使用JavaScript驱动的转换器应用程序中工作,现在我正在尝试使用所有措施来创建一个巨大的对象。 But whenever I debug it, it's saying things like: 但是,每当我调试它时,它都会说类似以下内容:

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

It's been a long time since I coded in JS, so I'm having some confusion with it, would appreciate any help, here's the code: 自从我用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;

...

Only use var to declare variables, not to create properties of an existing object: 仅使用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;

Also consider 还考虑

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: {}
};

No var before attributes, only variables. 属性前没有var ,只有变量。

var units = {
    length: {},
    time: {},
    mass: {},
    temperature : {}
};

NB: length is reserved to array/string length, you should avoid to name an attribute like that. 注意:length保留为数组/字符串的长度,应避免这样命名属性。 And you should use an extend method to avoid to repeat units and units.length . 并且您应该使用扩展方法来避免重复unitsunits.length

var units = {
    length: {
        meter: 1,
        meters: 1,
        inch: 0.0254,
        inches: 0.0254 // ...
    },
    time: {},
    mass: {},
    temperature : {}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM