Function overloading in Javascript - Best practices

后端 未结 30 2061
难免孤独
难免孤独 2020-11-22 03:33

What is the best way(s) to fake function overloading in Javascript?

I know it is not possible to overload functions in Javascript as in other languages. If I neede

30条回答
  •  庸人自扰
    2020-11-22 03:57

    I would like to share a useful example of overloaded-like approach.

    function Clear(control)
    {
      var o = typeof control !== "undefined" ? control : document.body;
      var children = o.childNodes;
      while (o.childNodes.length > 0)
        o.removeChild(o.firstChild);
    }
    

    Usage: Clear(); // Clears all the document

    Clear(myDiv); // Clears panel referenced by myDiv

提交回复
热议问题