Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the method:</p> <pre><code>if (module &amp;&amp; module.foo &amp;&amp; module.foo.bar &amp;&amp; module.foo.bar.baz) </code></pre> <p>still remains the best to test for a valid path in a object. That said, I think you could use something like that to test a valid object path:</p> <pre><code>var safeObjectPath = function safeObjectPath( object, properties ) { var path = [], root = object, prop; if ( !root ) { // if the root object is null we immediately returns return false; } if ( typeof properties === 'string' ) { // if a string such as 'foo.bar.baz' is passed, // first we convert it into an array of property names path = properties ? properties.split('.') : []; } else { if ( Object.prototype.toString.call( properties ) === '[object Array]' ) { // if an array is passed, we don't need to do anything but // to assign it to the internal array path = properties; } else { if ( properties ) { // if not a string or an array is passed, and the parameter // is not null or undefined, we return with false return false; } } } // if the path is valid or empty we return with true (because the // root object is itself a valid path); otherwise false is returned. while ( prop = path.shift() ) { // Before it was used an if..else statement only, but it // could generate an exception in case of inexistent // object member. We can fix it using a try..catch // statement. Thanks to @xecute for the contribution! try { if ( prop in root ) { root = root[prop]; } else { return false; } } catch(e) { return false; } } return true; } </code></pre> <p>The function will accept a <strong>string</strong> or an <strong>array</strong> as <code>properties</code> value. Internally the parameter is converted to an array and tested for the path.</p> <p>Properties can be specified as <code>'foo.bar.baz'</code> or <code>['foo','bar','baz']</code>.</p> <p>To test for a valid path:</p> <pre><code>safeObjectPath( module ) safeObjectPath( module, 'foo.bar.baz' ) safeObjectPath( module, [ 'foo', 'bar', 'baz' ] ) </code></pre> <p>Please, note that the first form (the one without <code>properties</code> parameter) will returns <code>true</code> (if passed root object is valid, of course) since that <code>module</code> is a valid path (the root).</p> <p>It's possibile to test it with this working <a href="http://jsfiddle.net/ragnarokkr/begck/" rel="nofollow noreferrer">fiddle</a>.</p> <hr> <p><del>I believe it would be possibile to think at a recursive and/or bindable version, too.</del></p> <p><strong>EDIT:</strong> I've extended this answer with <a href="https://coderwall.com/p/cvbgia" rel="nofollow noreferrer">a more detailed analysis</a> in my article published on Coderwall.</p> <p>Here's a <a href="http://jsperf.com/isobjectdefinedproto" rel="nofollow noreferrer">performance test</a> built by <a href="https://stackoverflow.com/users/958632/xecute">@xecute</a> (Thanks again for your efforts).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload