• Blogs (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • JS self-defined log function

    Blogs20142014-06-09


    JS self-defined log function

    In a developement/product environment, to more precisely use console.log(), I wrote the following which is more clean, and efficient:

        `//1. JS version:

    var log=(function() { var debug=/(localhost|192.168.|127.0.0)/. test(location.host) && !!window.console; return function() { return debug ? console.log.apply(console, arguments) : null; } })(); //then: log({}, [], ‘string’, Math.random()); log(JSON.stringify(Object.prototype))

    //2. jQuery version: $.extend({ log: (function() { var debug=/(localhost|192.168.|127.0.0)/. test(location.host) && !!window.console; return function() { if(debug) { return console.log.apply(console, arguments); } } }()) }); //then: $.log(‘json’, [], {}, Math.random()); $.log(JSON.stringify({a:1,b:2,c:3}), {a:1,b:2,c:3},[1,2,3], [{},{}])`

    How to merge 1 array object into a certain place of another with continuous increased index? The array could be complex JSON object. The following is a good way:

        `Array.prototype.splice.apply(ary1, [1, 0].concat(ary2));`