Javascript AND operator within assignment

后端 未结 6 695
滥情空心
滥情空心 2020-11-22 05:20

I know that in JavaScript you can do:

var oneOrTheOther = someOtherVar || \"these are not the droids you are looking for...\";

where the va

6条回答
  •  抹茶落季
    2020-11-22 05:59

    Beginners Example

    If you are trying to access "user.name" but then this happens:

    Uncaught TypeError: Cannot read property 'name' of undefined 
    

    Fear not. You can solve this using the && operator in an assignment or often called the guard operator since it "guards" from the undefined error happening.

    Here are some examples you may find odd but keep reading as it is explained later.

    var user = undefined;
    var username = user && user.username;
    // no error, "username" assigned value of "user" which is undefined
    
    user = { username: 'Johnny' };
    username = user && user.username;
    // no error, "username" assigned 'Johnny'
    
    user = { };
    username = user && user.username;
    // no error, "username" assigned value of "username" which is undefined
    

    Explanation: In the guard operation, each term is evaluated left-to-right one at a time. If a value evaluated is falsy, evaluation stops and that value is then assigned. If the last item is reached, it is then assigned whether or not it is falsy.

    falsy means it is any one of these values undefined, false, 0, null, NaN, '' and truthy just means NOT falsy.

    Easy button for Guard Method

    Use lodash's beautiful guard operator to access nested data like so:

    // need to access obj.has.some.very.nested.stuff
    var val = _.get(obj, 'has.some.very.nested.stuff');
    

    as it handles the && stuff under the hood while being nice to use in a convenient method. Lodash _.get source code

    Bonus: The OR Operator

    The other useful strange assignment that is in practical use is the OR operator which is typically used for plugins like so:

    this.myWidget = this.myWidget || (function() {
       // define widget
    })();
    

    which will only assign the code portion if "this.myWidget" is falsy. This is handy because you can declare code anywhere and multiple times not caring if its been assigned or not previously, knowing it will only be assigned once since people using a plugin might accidentally declare your script tag src multiple times.

    Explanation: Each value is evaluated from left-to-right, one at a time. If a value is truthy, it stops evaluation and assigns that value, otherwise, keeps going, if the last item is reached, it is assigned regardless if it is falsy or not.

    Extra Credit: Combining && and || in an assignment

    You now have ultimate power and can do very strange things such as this very odd example of using it in a palindrome.

    function palindrome(s,i) {
     return (i >= s.length/2) || (s[i] === s[s.length -1 - i]) && palindrome(s, ++i);
    }
    

    In depth explanation here: Palindrome check in Javascript

    Happy coding.

提交回复
热议问题