js如何在严格模式中查看调用者信息?

新手上路,请多包涵

问题描述

我使用Vue框架编码,由于vue单文件特性,vue编码自动变为严格模式。所以我在其他js实现了一个自己的platform_errlog,但是由于不能在严格模式下使用arguments进行查看调用者的操作了

问题出现的环境背景及自己尝试过哪些方法

我的errolog.js并没有使用严格模式,是否改成非ES6 module输出的模式,那边输入也用require就能避开严格模式?我没法在我的errlog里面实现链路追踪,请问各位大神是否有办法解决此问题?拜谢!

相关代码

export function errlog(log_info) {
  console.log("========================================");
  console.log("| lg_admin错误信息:");
  console.log("| ",log_info);
  console.log("----------------------------------------");
}

export function err_entity_log(info, data) {
  console.log("========================================");
  console.log("| lg_admin统一错误处理:");
  console.log("| ",info);
  console.log("| ",data);
  console.log("----------------------------------------");
}

export function log_entity(info, data) {
  console.log("========================================");
  console.log("| lg_admin_log:");
  console.log("| ",info);
  console.log("| ",data);
  console.log("----------------------------------------");
}

你期待的结果是什么?实际看到的错误信息又是什么?

实际错误信息贴一下:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions
阅读 3.2k
1 个回答

我做过类似的事情,我的解决方案是:

如果你的目标函数可以自由修改,可以

try{
    throw new Error("Caller Satck");
}catch(e){
    console.log(e.stack)
}

,如果目标函数不能修改,那就使用一个proxy之类的东西,代理一下,同样进行这种操作

我的那篇博客 严格模式下取得函数调用栈

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题