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.
-
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 valueundefined
), where that won't have the same behaviour. You could just declare something likevar 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
3 Answers
Reset to default 5No, 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条)