So now I nailed down the basics of javascript and I'm ready to get into the more intermediate arts of coding with "style". I'm trying to write easy maintainable code. The idea is to make a function work even if one of the object properties in use is not available by creating fallbacks. Problem is if I access the properties often then I would have to create ternary conditionals that create a simple for each accessed property. Right now you can see I'm only accessing object.a
. I could of course store all the accessing of properties:
Idea 1
var a = (object.hasOwnProperty(a) ? object.a : a)
var b ...
var c ...
idea 2:
var a = (object['a'] ? object.a : a)
idea 3:
var a = object['a'] ? object.a : a
Idea 3:
(object.hasOwnProperty(a) ? var a = object.a : var a = 1);
Idea 4:
Switch statements?
At last:
object = {
// a: 1,
b: 2,
c: 3,
}
// normal vars in case one of the properties do not exist
var a = 1,
b = 2,
c = 3;
function x(){
var a = 1;
object.a * 10
if (object.a == not exist || (object.b == not exist|| (object.c == not exist)
then treat all non existing object properties accessed to as normal variables by doing:
convert object.a --> a
{