ANDing javascript objects together

Posted by Jonas on Stack Overflow See other posts from Stack Overflow or by Jonas
Published on 2010-04-06T18:07:30Z Indexed on 2010/04/06 18:23 UTC
Read the original article Hit count: 361

Filed under:

I ran across this chunk of code (modified) in our application, and am confused to how it works:

    function someObject()
    {
        this.someProperty = {};
        this.foo = 
        {
            bar:
            {
                baz: function() { return "Huh?" }
            }
        };

        this.getValue = function()
        {
            return (this.someProperty && this.foo.bar && this.foo.bar.baz && this.foo.bar.baz()) || null;
        }
    }

    function test()
    {
        var o = new someObject();
        var val = o.getValue();
        alert(val);
    }

when you call the test() function, the text "Huh?" is alerted. I'm not sure how the result of getValue is returning that, I would've thought doing A && B && C && D would have returned true, rather than the value of D.

© Stack Overflow or respective owner

Related posts about JavaScript