function(){}; do?

This question already has an answer here:

  • Javascript || operator 5 answers
  • Is there a better way to do optional function parameters in JavaScript? [duplicate] 29 answers

  • In words:

    if there is no processMethod, create it empty.
    

    || works with booleans, so it checks if the first operand processMethod has a boolean-equivalent. If processMethod is defined and not null, the boolean-equivalent is true . If processMethod is undefined or null, the boolean-equivalent is false . In the false-case, || looks for a boolean-equivalent of the second-operand, its not null so its boolean-equivalent is true .

    false || true false || true resolves to true so processMethod becomes function(){} .

    Btw function(){} is a empty function whom used to not throw a error on processMethod()


    It essentially checks whether it exists or not. If it doesn't exist, assign it.

    function doSomething(o) {
        o = o || {};
    }
    

    In the above case, it checks whether a value for o was passed. If not it assigns an empty object to it.

    链接地址: http://www.djcxy.com/p/17286.html

    上一篇: 如何在函数中处理可选参数

    下一篇: 功能(){}; 做?