你不知道的 JavaSciprt 调试方法

//  You Know JavaScript Debugger.
module.exports = (cls) => {
    if (typeof(cls) === 'function') {
        if (cls.prototype) {
            try {
                cls.arguments && cls.caller;
                return false;
            } catch (e) {}
                return true;
        }
    }
    return false;
}
/* ~All parts of a ClassDeclaration or a ClassExpression are strict mode code. */
/* 说明class声明下都是strict mode, 那么在严格模式下,你一定见过这个错误:
*  Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
*  所以如果你不是这么定义一个函数:
* */
"use strict";
var f = function () {};
/*
*  普通函数的f.caller和f.arguments都是可以访问的.但是ckass的话,不加'use strict'也默认是严格模式,上面发的库就是利用了这个特性来进行isClass实现.
*  SO: 一般情况下这个判断是没有错误的,又看了一个另外的判断class的库:
* */
var toString = Function.prototype.toString();
function fnBody (fn) {
    return toString.call(fn).replace(/^[^{]*{\s*/, '').replace('/\s*}[^}]*s/', '');
}
function isClass(fn) {
    return (typeof fn === 'function' && (/^class(\s|\{\}$)/.test(toString.call(fn)) || (/^.*classCallCheck\(/.test(fnBody(fn)))) // bobel.js
    );
}
/*
*  这个就比较粗暴了,直接toString之后看有没有class关键字,如果是babel转过去的class会是这样的.
* */
"use strict";
function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
var a = function a() {
    _classCallCheck(this, a);
}

  

转载于:https://www.cnblogs.com/phpcurd/p/9605671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值