javascript - Should I return a method call like console.log()? -


good day everyone. haven't found research or articles regarding this. maybe using wrong search terms. question lets example writing small utility library in javascript myself , want include 2 small functions out(arg) , outln(arg). question should call console.log() or return console.log(). i've seen done both ways never saw reason return method call. wondering way better or make difference @ all?

ex.

// way:  var lib = {     out: function(args){         console.log(args);     },     outln: function(args){         console.log(args + "\n");     } };  // or way?  var lib = {     out: function(args){         return console.log(args);     },     outln: function(args){         return console.log(args + "\n");     } }; 

do have use return value console.log (which undefined anyway iirc ) ? might plan override log or create own console object, though (not recommended).

more important, check presence of console object , log method, user agents have been known not provide them:

var lib = {     out: function(args) {              if (console && console.log) {                  console.log(args);              }          },     outln: function(args) {                if (console && console.log) {                    console.log(args+"\n");                }          } } 

promoting robustness, test way instead of once during lib's initialization , setting global ( might consider add test lib if have use case, eg. users/code tampering console object ).


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -