Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set undefined javascript property before read

var tr={};
tr.SomeThing='SomeThingElse';
console.log(tr.SomeThing); // SomeThingElse
console.log(tr.Other); // undefined

tr.get=function(what){
    if (tr.hasOwnProperty(what)) return tr[what];
    else return what;
};
tr.get('SomeThing') // SomeThingElse
tr.get('Other') // Other

Is there any way to make tr.Other or tr['Other'] and all other undefined properties of the object to return its name instead undefined?

like image 510
rndm Avatar asked Jul 16 '12 11:07

rndm


People also ask

How can we avoid the undefined property Cannot be read?

How to Avoid TypeError: Cannot Read Property of Undefined. To avoid coming across situations where undefined variables may be accessed accidentally, an if check should be added before dealing with such variables: if (myVar !== undefined) { ... } if (typeof(myVar) !==

Can you assign undefined in JavaScript?

YES, you can, because undefined is defined as undefined.

How are properties of undefined set?

The "Cannot set properties of undefined" error occurs when setting a property on an undefined value. To solve the error, conditionally check if the value is of the expected type (object or array) or has to be initialized before setting the property on it. Here is an example of how the error occurs.

Can not read the property of undefined reading get?

The "Cannot read properties of undefined" error occurs when trying to access a property on an undefined value. You often get undefined values when: accessing a property that does not exist on an object. accessing an index that is not present in an array.


1 Answers

Three solutions:

  • Implement your object as a Proxy, which is designed to do exactly what you want. Yet, it is only a draft and currently only supported in Firefox' Javascript 1.8.5 It was standardised with ES6, but might not yet be available in all environments.

  • Always fill your translation object with a complete set of messages. When creating that "dictionary" (serverside or clientside), always include all needed keys. If no translation exists, you can use a fallback language, the message's name or the string representation of undefined - your choice.

    But a non-existing property should always mean "there is no such message" instead of "no translation available".

  • Use a getter function with a string parameter instead of object properties. That function can look the messages up in an internal dictionary object, and handle misses programmatically.

    I would recommend a map object which is different from the dictionary, to allow "get" and co as message names:

var translate = (function(){
    var dict = {
        something: "somethingelse",
        ...
    };
    return {
        exists: function(name) { return name in dict; },
        get: function(name) { return this.exists(name) ? dict[name] : "undefined"; },
        set: function(name, msg) { dict[name] = msg; }
    };
})();
like image 139
Bergi Avatar answered Nov 03 '22 15:11

Bergi