How to create a CoffeeScript style existential operator in JavaScript? - Stack Overflow

CoffeeScript turns user?.id intoif (typeof user !== "undefined" && user !== null) {us

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.

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.

Share Improve this question asked Jun 27, 2013 at 11:05 thethe 22k12 gold badges74 silver badges102 bronze badges 5
  • 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 Commented Jun 27, 2013 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 Commented Jun 27, 2013 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 Commented Jun 27, 2013 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 Commented Jun 27, 2013 at 12:55
  • 1 Checkout the lodash get function. – tusharmath Commented Oct 17, 2015 at 4:59
Add a ment  | 

3 Answers 3

Reset to default 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/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/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.

Very old question but made me thinking about this solution.

exists = (obj) => obj || {}
exists(nullableObject).propName;

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 plete 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744227608a4564085.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信