9

CoffeeScript turns user?.id into

if (typeof user !== "undefined" && user !== null) {
   user.id;
}

Is it possible to create a JavaScript function exists that would do something similar? i.e.

exists(user).id

would result in either user.id or null

It would be easier if a function accepts another parameter, i.e. exists(user, 'id'), but that wouldn't look as nice.

kqw
  • 17,649
  • 11
  • 61
  • 92
  • For this one would need general purpose getters (`definegetter(o, function(property){...})`, which are, if I recall correclty, not part of JavaScript's specification. – Zeta Jun 27 '13 at 11:19
  • @Zeta Not really, since accessing a nonexistent property of an object returns `undefined` instead of failing, which should be good enough. – millimoose Jun 27 '13 at 11:24
  • 3
    The closest I can think of is `function exists(obj) { if (obj) return obj; return {}; }`, but that won't handle undefined variables. Javascript isn't syntactically extensible that way. – millimoose Jun 27 '13 at 11:24
  • You'd have an issue with `user` being not defined (different from having the value `undefined`), where that won't have the same behaviour. You could just declare something like `var user;` for the sake of it, then it'd be fine but it creates quite a bit of clutter. – Qantas 94 Heavy Jun 27 '13 at 12:55
  • 1
    Checkout the lodash get function. – tusharmath Oct 17 '15 at 04:59

3 Answers3

5

No, you can't produce such a function. The problem is that this:

any_function(undeclared_variable)

will produce a ReferenceError if undeclared_variable was not declared anywhere. For example, if you run this stand alone code:

function f() { }
f(pancakes);

you'll get a ReferenceError because pancakes was not declared anywhere. Demo: http://jsfiddle.net/ambiguous/wSZaL/

However, the typeof operator can be used on something that has not been declared so this:

console.log(typeof pancakes);

will simply log an undefined in the console. Demo: http://jsfiddle.net/ambiguous/et2Nv/

If you don't mind possible ReferenceErrors then you already have the necessary function in your question:

function exists(obj, key) {
    if (typeof obj !== "undefined" && obj !== null)
        return obj[key];
    return null; // Maybe you'd want undefined instead
}

or, since you don't need to be able to use typeof on undeclared variables here, you can simplify it down to:

function exists(obj, key) {
    if(obj != null)
      return obj[key];
    return null;
}

Note the change to !=, undefined == null is true even though undefined === null is not.

mu is too short
  • 396,305
  • 64
  • 779
  • 743
2

Very old question but made me thinking about this solution.

exists = (obj) => obj || {}
exists(nullableObject).propName;
Sudheer
  • 329
  • 2
  • 4
  • nope. To quote coffeescript.org: "It’s a little difficult to check for the existence of a variable in JavaScript. if (variable) … comes close, but fails for zero, the empty string, and false (to name just the most common cases)" – JasonWoof Apr 02 '18 at 21:51
0

I think this functional approach might be interesting until optional chaining is included in JavaScript (State 1 of TC39):

Using proxies and a Maybe monad, you can implement optional chaining with default value return in case of failure.

A wrap() function is used to wrap objects on which you want to apply optional chaining. Internally, wrap creates a Proxy around your object and manages missing values using a Maybe wrapper.

At the end of the chain, you unwrap the value by chaining getOrElse(default) with a default value which is returned when the chain is not valid:

const obj = {
  a: 1,
  b: {
    c: [4, 1, 2]
  },
  c: () => 'yes'
};

console.log(wrap(obj).a.getOrElse(null)) // returns 1
console.log(wrap(obj).a.b.c.d.e.f.getOrElse(null)) // returns null
console.log(wrap(obj).b.c.getOrElse([])) // returns [4, 1, 2]
console.log(wrap(obj).b.c[0].getOrElse(null)) // returns 4
console.log(wrap(obj).b.c[100].getOrElse(-1)) // returns -1
console.log(wrap(obj).c.getOrElse(() => 'no')()) // returns 'yes'
console.log(wrap(obj).d.getOrElse(() => 'no')()) // returns 'no'

wrap(obj).noArray.getOrElse([1]).forEach(v => console.log(v)) // Shows 1
wrap(obj).b.c.getOrElse([]).forEach(v => console.log(v)) // Shows 4, 1, 2

The complete example:

class Maybe {
  constructor(value) {
    this.__value = value;
  }
  static of(value){
    if (value instanceof Maybe) return value;
    return new Maybe(value);
  }
  getOrElse(elseVal) {
    return this.isNothing() ? elseVal : this.__value;
  }
  isNothing() {
    return this.__value === null || this.__value === undefined;
  }
  map(fn) {  
    return this.isNothing()
      ? Maybe.of(null)
      : Maybe.of(fn(this.__value));
  }
}

function wrap(obj) {
  function fix(object, property) {
    const value = object[property];
    return typeof value === 'function' ? value.bind(object) : value;
  }
  return new Proxy(Maybe.of(obj), {
    get: function(target, property) {
      if (property in target) {
          return fix(target, property);
      } else {
        return wrap(target.map(val => fix(val, property)));
      }
    }
  });
}

const obj = { a: 1, b: { c: [4, 1, 2] }, c: () => 'yes' };

console.log(wrap(obj).a.getOrElse(null))
console.log(wrap(obj).a.b.c.d.e.f.getOrElse(null))
console.log(wrap(obj).b.c.getOrElse([]))
console.log(wrap(obj).b.c[0].getOrElse(null))
console.log(wrap(obj).b.c[100].getOrElse(-1))
console.log(wrap(obj).c.getOrElse(() => 'no')())
console.log(wrap(obj).d.getOrElse(() => 'no')())

wrap(obj).noArray.getOrElse([1]).forEach(v => console.log(v)) // Shows 1
wrap(obj).b.c.getOrElse([]).forEach(v => console.log(v)) // Shows 4, 1, 2
jo_va
  • 11,779
  • 3
  • 20
  • 39