Fawar
Fawar

Reputation: 735

Dictionary for missing value

Here is the code

converter = 
{
    'SIToImperial' : 
    {
        'cm'    : function(value) {return value * convertRatioImperial[0];},
        'm'     : function(value) {return value * convertRatioImperial[1];},
        'km'    : function(value) {return value * convertRatioImperial[2];},
        'g'     : function(value) {return value * convertRatioImperial[3];},
        'kg'    : function(value) {return value * convertRatioImperial[4];},
        't'     : function(value) {return value * convertRatioImperial[5];},
        'mL'    : function(value) {return value * convertRatioImperial[6];},
        'L'     : function(value) {return value * convertRatioImperial[7];},
        'm3'    : function(value) {return value * convertRatioImperial[8];},
        'kWh'   : function(value) {return value;},
        'nb'    : function(value) {return value;},
        'undefined': function(value) {return 'Not Found';}
    }
}

It is clear that my line 'undefined'.... does not work like I want it to.

I would like that when converter["SIToImperial"][units] is called with an 'units' not listed in the properties. Exemple : converter["SIToImperial"]['oz']. It should return the undefined line IE 'Not Found'.

Could someone help me out I have tried various ways but I'm still not enough familiar with dictionnaries to get it working properly.

Update :

//Generalized conversion function
function convert(value,valueUnit,system, toSI)
{
    var result;
    //From SI to Imp/U
    if(!toSI)
    {
        result = converter.guardian('SITo'+system,valueUnit,value);
    }
    else if(toSI)//To SI from Imp/US
    {
        result = converter.guardian(system+'ToSI',valueUnit,value);
    }
    return result;
};

and

converter = 
{

    guardian    :   function (system,units,value) {var label = this[system][units]; if(typeof(label) === 'undefined') {return "Not Found";} else {return label(value);}},
    'SIToImperial' : 
    {
        'cm'    : function(value) {return value * convertRatioImperial[0];},
        'm'     : function(value) {return value * convertRatioImperial[1];},
        'km'    : function(value) {return value * convertRatioImperial[2];},
        'g'     : function(value) {return value * convertRatioImperial[3];},
        'kg'    : function(value) {return value * convertRatioImperial[4];},
        't'     : function(value) {return value * convertRatioImperial[5];},
        'mL'    : function(value) {return value * convertRatioImperial[6];},
        'L'     : function(value) {return value * convertRatioImperial[7];},
        'm3'    : function(value) {return value * convertRatioImperial[8];},
        'kWh'   : function(value) {return value;},
        'nb'    : function(value) {return value;}
    }
}

Upvotes: 0

Views: 101

Answers (3)

Bergi
Bergi

Reputation: 665130

You can't do that. Instead of defining a "default" property on the object, you have to set up a default behaviour for when accessing the object fails. You don't even need a guardian method, you can do it right in the convert function:

function convert(value,valueUnit,system, toSI) {
    return ( converter[ toSi 
               ? system+'ToSI'
               : 'SITo'+system
             ][valueUnit]
           || function() {
                  return 'Not Found';
              }
           ) (value);
}

or

function convert(value,valueUnit,system, toSI) {
    var units = converter[ toSi ? system+'ToSI' : 'SITo'+system ];
    return valueUnit in units
            ? units[valueUnit](val)
            : 'Not Found';
}

Upvotes: 0

JaredPar
JaredPar

Reputation: 755269

I don't believe there is a way to achieve your goal here. You are essentially trying to hook the case where a member is asked for that doesn't exist. Such a hook doesn't exist in javascript. Instead I think you'll need to use a function here that wraps the type

converter = 
{
    'SIToImperial' : 
    {
        ... 
        getMember = function (name) {
          var value = this[name];
          if (typeof(value) === 'undefined') { 
            return 'Not Found'
          }
          return value;
    }
}

Upvotes: 1

Halcyon
Halcyon

Reputation: 57703

Not a really nice solution but quick and easy:

var my_conv = converter.SIToImperial[units] || converter.SIToImperial["undefined"];
console.log(my_conv(value));

Side note:

converter[SIToImperial] // is probably wrong, you either mean:
converter["SIToImperial"] // or
converter.SIToImperial

Upvotes: 1

Related Questions