Reputation: 3222
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
{
Upvotes: 3
Views: 7649
Reputation:
ES6 provides nice facilities for doing what you want. The simplest example is
var { a = 1, b = 2 } = obj;
This uses destructuring assignment with defaults. The property a
on obj
is retrieved and assigned to variable a
, but it if doesn't exist, a
takes on the value 1.
Upvotes: 6
Reputation: 75660
The ternary operator is a great way to do this. It allows you the ability to evaluate any expression you like to determine whether your candidate value is appropriate or whether you should use a fallback.
Examples...
// Use fallback if candidate is not "truthy"
var result = obj.a ? obj.a : "fallback";
// Use fallback if candidate is undefined
var result = obj.a !== undefined ? obj.a : "fallback";
// Use fallback if candidate is not defined on the object (whether or not it exists in the prototype chain)
var result = obj.hasOwnProperty(a) ? obj.a : "fallback";
You need to decide what condition you'd like to use a fallback value. Once you decide, wrap it in a function. Or make several similar functions which use different conditions.
Here's a function which checks to see if the candidate value is undefined and returns a fallback value.
function getOrDefault(candidate, fallback) {
if (typeof candidate === "undefined") {
return fallback;
}
return candidate;
}
// Example 1
var b = "alternate value";
var obj = { foo: "value" };
var result = getOrDefault(obj.a, b);
// result -> "alternate value";
// Example 2
var b = "alternate value";
var obj = { a: false };
var result = getOrDefault(obj.a, b);
// result -> false;
Also worth looking into is lodash's get function. Allows you to check for the existence of a property (even deeply nested properties) and allows you to specify a fallback.
Upvotes: 7
Reputation: 107566
Expanding on my comments about an "extend" or "merge" function, let's take a look at the following (from here):
var extend = function ( defaults, options ) {
var extended = {};
var prop;
for (prop in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, prop)) {
extended[prop] = defaults[prop];
}
}
for (prop in options) {
if (Object.prototype.hasOwnProperty.call(options, prop)) {
extended[prop] = options[prop];
}
}
return extended;
};
// Your "fallback" object values
var defaults = {
a: 1,
b: 2
};
// Your target object
var myObject = {
b: 4,
c: 8
};
// one line to override a set of defaults, producing a final object
console.log(extend(defaults, myObject)); // Object { a: 1, b: 4, c: 8 }
Upvotes: 1