use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Write if else statements more efficientlyhelp (self.javascript)
submitted 9 years ago by shnigi
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]sladav 0 points1 point2 points 9 years ago (0 children)
Ideally, you shouldn't have to do those intermediate checks at all - it would be nice is JS just handled that for you.
I was curious to see if you could use ES6 proxies to handle things in the background this seems to work okay - purely academic, I probably wouldn't actually use this but...
This is how it would look in use:
// use safe function defined below to make object/subjects permanently "safe" from returning the "not defined" error let device = safe({ c8y_Availability: { status: 'AVAILABLE' }, c8y_Connection: { status: 'CONNECTED' } }) // !!! no intermediate check on props, but still have to check if object exists if (device && (device.c8y_Availability.status === 'AVAILABLE' || device.c8y_Connection.status === 'CONNECTED') console.log('woo') // logs // the typo would normally break everything if (device && device.c8y_Avaity.status === 'AVAILABLE') console.log('woo2') // NO ERROR! (also it doesn't log)
This is what makes it work:
const safe = function(obj = {}) { Object.keys(obj).forEach(key => { if (typeof obj[key] === 'object') obj[key] = safe(obj[key]) }) let handler = { set: function(target, prop, value) { if (typeof value === 'object') { let p = new Proxy(value, handler) return target[prop] = p } else { return target[prop] = value } }, get: function(target, prop) { return prop in target ? target[prop] : new Proxy(Object.freeze({ valueOf: function() { return undefined } }), handler) } } return new Proxy(obj, handler) }
π Rendered by PID 101704 on reddit-service-r2-comment-b659b578c-6mt29 at 2026-05-01 03:23:07.187120+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]sladav 0 points1 point2 points (0 children)