Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: check for obj nested prop Date: Sat, 09 May 2020 03:09:00 +0100 Organization: A noiseless patient Spider Lines: 26 Message-ID: <873689yi4j.fsf@bsb.me.uk> References: <87pnbeycrm.fsf@bsb.me.uk> <11518358.O9o76ZdvQC@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="8b3d9a98c74a7c42702bb3ceb61230f2"; logging-data="16333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19u8vX1PV/AMyNvwWEniMUtZFqP9FLrw1k=" Cancel-Lock: sha1:gdg9YmNC5GSfuWl4ewNxWJwX8S8= sha1:+i45B7HhwmKueOmPP87ehPusPsM= X-BSB-Auth: 1.33e17b8d2c73faabee52.20200509030900BST.873689yi4j.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:38394 Thomas 'PointedEars' Lahn writes: > Ben Bacarisse wrote: > >> Andrew Poulos writes: >>> so I end up doing >>> >>> if (a && a.b && a.b.c && a.b.c.d) >> >>> Is there a way to get the check value of an object's nested property >>> without first having to test for all the ascendant properties? >> >> if (try {obj.a.b.c.d} catch {}) >> >> might do what you want. > > This is *obviously* syntactically invalid; Yes, wishful thinking. You'd need a function call and a return: if ( (()=>{ try {return obj.a.b.c.d} catch {} })() ) ... which makes it rather clumsy. -- Ben.