简体   繁体   English

如何将 console.log 绑定到另一个 function 调用,以便我可以在调用它的控制台中看到脚本的行号?

[英]How to bind console.log to another function call so I can see line number of the script in console where it is called?

My code works but with additional parenthesis like myfunction()();我的代码有效,但有额外的括号,如myfunction()(); . . It should execute with single parenthesis just like normal eg myfunction();它应该像正常情况一样用单括号执行,例如myfunction(); . .

I'm building console.time(); console.timeEnd();我正在构建console.time(); console.timeEnd(); console.time(); console.timeEnd(); polyfill for browsers (eg <IE10) which do not have native built-in.用于没有本机内置的浏览器(例如 <IE10)的 polyfill。 Note: I have bind() polyfill in-case you think <IE10 does not have it.注意:我有 bind() polyfill 以防你认为 <IE10 没有它。

Here is my code in "polyfill.js file".这是我在“polyfill.js 文件”中的代码。

(function() {
    'use strict';
    var console=window.console,  timers={};
    
    if (!console.time) {
        
        console.time = function(name) {
            var datenow = Date.now();
            name = name? name: 'default';
            if (timers[name]) {
                console.warn('Timer "'+name+'" already exists.');
            }
            else timers[name] = datenow;
        };
        
        console.timeEnd = function(name) {
            var datenow = Date.now();
            name = name? name: 'default';
            if (!timers[name]) {
                console.warn('Timer "'+name+'" does not exists.');
            }
            else {
                var endt = datenow - timers[name];
                delete timers[name];
                //below is the line where some changes are needed, But I don't know how.
                return window.console.log.bind(window.console, name+ ': ' +endt+ 'ms');
            }
        };
    }
}());

Now in another file "main.js file", when I use console.time(); console.timeEnd();现在在另一个文件“main.js 文件”中,当我使用console.time(); console.timeEnd(); console.time(); console.timeEnd(); , it should log code-line-number of this file in browser console (not the line-number of polyfill.js file). ,它应该在浏览器控制台中记录此文件的代码行号(而不是 polyfill.js 文件的行号)。 Of-course it works but notice additional parenthesis "()()" below which is not cool.当然它可以工作,但请注意下面的附加括号“()()”并不酷。

console.time();
//any code for performance test goes here.
console.timeEnd()(); //Note here "()()". It should be single "()"

I have consulted these 2 stackoverflow questions, but couldn't come up with the right answer.我已经咨询了这 2 个 stackoverflow 问题,但无法得出正确答案。

I also checked new Error().stack;我还检查了new Error().stack; as an option, but it is also not supported in those browser for which I'm building my polyfill.作为一个选项,但我正在为其构建 polyfill 的那些浏览器也不支持它。

Note: If anyone can suggest a solution with eval();注意:如果有人可以用eval(); , you can. , 你可以。 It is also acceptable for me.这对我来说也是可以接受的。

There is in fact a function for that called console.trace , which you can read more about in the MDN page .实际上有一个名为console.trace的 function,您可以在MDN 页面中阅读更多相关信息。 What it does is print the entire stack trace to the line where it has been called from.它所做的是将整个堆栈跟踪打印到调用它的行。

So, for example, running the next code:因此,例如,运行下一个代码:

function firstFunc() {
  secondFunc();
}

function secondFunc() {
  console.trace('I was called here!');
}

console.log('Calling firstFunc:');
firstFunc();

will print out this output in the console:将在控制台中打印出这个 output:

Calling firstFunc:

I was called here!
secondFunc @ VM141:6
firstFunc @ VM141:2
(anonymous) @ VM141:10 // Internal browser trace

Notice that in the given output, all functions are being called and defined in the Chrome console, hence the @ VM141: .请注意,在给定的 output 中,所有函数都在 Chrome 控制台中被调用和定义,因此@ VM141: Generally, it prints the file instead of VM.通常,它打印文件而不是 VM。 So, had it been located in an index.js file, it would look like this:因此,如果它位于index.js文件中,它将如下所示:

Calling firstFunc:

I was called here!
secondFunc @ index.js:8

Compatibility Note兼容性说明

The above method works for any sane browser, and IE11+.上述方法适用于任何健全的浏览器,以及 IE11+。 That is due to the implementation of console.trace only in IE11.这是由于仅在 IE11 中实施了console.trace However, per OP's request, I can think of a creative way to support IE10, and that is by using the Error.prototype.stack property .但是,根据 OP 的要求,我可以想到一种支持 IE10 的创造性方法,那就是使用Error.prototype.stack属性 Now, of course, as MDN itself mentions it, it's a non-standard feature that should not be used in production, but neither is supporting IE6.现在,当然,正如 MDN 本身提到的那样,它是一个不应该在生产中使用的非标准功能,但也不支持 IE6。

By creating an Error instance and then printing its stack, you can achieve a similar result.通过创建一个 Error 实例然后打印它的堆栈,您可以获得类似的结果。

const sumWithTrace = (num1, num2) => {
  console.log(new Error().stack); // Creating a new error for its stack property
  return num1 + num2;
};

sumWithTrace(1, 5); // returns 6 and prints trace in console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM