6
\$\begingroup\$

I am currently wondering if the following piece of Javascript code could be simplified? I would like to return the correct {},[],"" without having to pass a type as argument. Is it possible?

function getObjectProperty(object,field,type){
    switch (type){
      case "Array":
        return object && object.hasOwnProperty(field) ? object[field] : [];
        break;
      case "String":
        return object && object.hasOwnProperty(field) ? object[field] : "";
        break;
      case "Object":
        return object && object.hasOwnProperty(field) ? object[field] : {};
        break;
      case "Boolean":
        return object && object.hasOwnProperty(field) ? object[field] : false;
        break;
    }
  }
\$\endgroup\$
9
\$\begingroup\$

1) I am currently wondering if the following piece of Javascript code could be simplified?

yes, something like this, would be more accurate and simpler to read

var defaultValues = {
  "Array": [],
  "Boolean": true,
  "Object": {},
  "String": ""
}

function getObjectProperty(object,field,type){
  if (object.hasOwnProperty(field)) return object[field]
  return defaultValues[type]
}

2) I would like to return the correct {},[],"" without having to pass a type as argument. Is it possible?

This is only possible, if the field exist.
Otherwise whats the type of foo in {bar: "bar"} ?

But what are you actually doing:

  1. check if the field is in the object
  2. If true, return the value, otherwise a defaultValue

So you could simplify it to this:

function getValue(obj, field, defaultValue) {
   return obj.hasOwnProperty(field) ? obj[field] : defaultValue
}
| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Javascript is loosely typed, it is possible to assign a variable with any type of object. As a result, it is not possible to remove the type argument from your function - you cannot infer that undefined is a String or a Number, because it can become anything.

Unfortunately, loose typing has significantly contributed to the production of bad javascript code, mainly for two reasons:

  1. Many programmers, especially library authors, have tried to write code generic enough to accept many types of values, often using argument overloading and type inference (known dubious practices). They failed to see that doing so would not only cripple the readability of the code, but also spread the belief that re-using variables for different types (different purposes) was okay.
  2. As a consequence, many programmers reacted viscerally against the type system of Javascript, preaching the use of strong typing, and even strict operators - rather than having people understand the actual type system of the language. In the end, the resulting code became needlessly complicated, and obscure side-effects were introduced.

Anyway, since you cannot do without the type, I suppose the best way to deal with your function is to actually pass the default value rather than the type. Ideally, you should try and design your code so that it does not need to infer variable types.

function getObjectProperty(target, property, defaultValue) {
    return target && target.hasOwnProperty(property) ? target[property] : defaultValue;
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.