What is the best safe way to use console.log and console.warn?

I am working on a code base that checks to console.warnexist as follows:

if (window.console) {                                                                                                                                                                                                                                                      
  console.warn("shhaabang");                                                                                                                                                                                                    
}                                                                                                                                                                                                                                                                          

However, my problem is that it only works in a browser where you have a window object. This seems to work too.

if (console && typeof console.warn == 'function') {                                                                                                                                                                                                                                                      
  console.warn("shhaabang");                                                                                                                                                                                                    
}

Are there any disadvantages of this method? Are there any better methods that don't exist window? Should I just see if a window or global creature exists and check both?

CLARIFICATION . At least it should be obvious that I am testing this in that it does not have an object window. But, to be explicit , I run this in node.js as well as in the browser

+4
2

javascript, , . , loglevel.

loglevel ( ):

function realMethod(methodName) {
    if (typeof console === undefinedType) {
        return false; // We can't build a real method without a console to log to
    } else if (console[methodName] !== undefined) {
        return bindMethod(console, methodName);
    } else if (console.log !== undefined) {
        return bindMethod(console, 'log');
    } else {
        return noop;
    }
}

function bindMethod(obj, methodName) {
    var method = obj[methodName];
    if (typeof method.bind === 'function') {
        return method.bind(obj);
    } else {
        try {
            return Function.prototype.bind.call(method, obj);
        } catch (e) {
            // Missing bind shim or IE8 + Modernizr, fallback to wrapping
            return function() {
                return Function.prototype.apply.apply(method, [obj, arguments]);
            };
        }
    }
}
+2

if(console && typeof console.warn == "function"){ ... }

,

this

:.

if(this.console){
  console.warn("shhaabang");
}

EDIT: , , 2 : 1) , this ... @ 2) strict mode, strict mode, this undefined

:

var c;
try {
  c = console;
} catch(e){ 
  // console is not defined
}

if(c) {
  // console is defined
}else{
  // this will not throw because you declared `c` earlier
}
0

Source: https://habr.com/ru/post/1612614/


All Articles