fei.wang
10 天以前 ae7b22322555448d95fd56f505bafa325c167a26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
 
var IsCallable = require('is-callable');
var hasOwn = require('hasown');
var functionsHaveNames = require('functions-have-names')();
var callBound = require('call-bound');
var $functionToString = callBound('Function.prototype.toString');
var $stringMatch = callBound('String.prototype.match');
var toStr = callBound('Object.prototype.toString');
 
var classRegex = /^class /;
 
var isClass = function isClassConstructor(fn) {
    if (IsCallable(fn)) {
        return false;
    }
    if (typeof fn !== 'function') {
        return false;
    }
    try {
        var match = $stringMatch($functionToString(fn), classRegex);
        return !!match;
    } catch (e) {}
    return false;
};
 
var regex = /\s*function\s+([^(\s]*)\s*/;
 
var isIE68 = !(0 in [,]); // eslint-disable-line no-sparse-arrays, comma-spacing
 
var objectClass = '[object Object]';
var ddaClass = '[object HTMLAllCollection]';
 
var functionProto = Function.prototype;
 
var isDDA = function isDocumentDotAll() {
    return false;
};
if (typeof document === 'object') {
    // Firefox 3 canonicalizes DDA to undefined when it's not accessed directly
    var all = document.all;
    if (toStr(all) === toStr(document.all)) {
        isDDA = function isDocumentDotAll(value) {
            /* globals document: false */
            // in IE 6-8, typeof document.all is "object" and it's truthy
            if ((isIE68 || !value) && (typeof value === 'undefined' || typeof value === 'object')) {
                try {
                    var str = toStr(value);
                    // IE 6-8 uses `objectClass`
                    return (str === ddaClass || str === objectClass) && value('') == null; // eslint-disable-line eqeqeq
                } catch (e) { /**/ }
            }
            return false;
        };
    }
}
 
module.exports = function getName() {
    if (isDDA(this) || (!isClass(this) && !IsCallable(this))) {
        throw new TypeError('Function.prototype.name sham getter called on non-function');
    }
    if (functionsHaveNames && hasOwn(this, 'name')) {
        return this.name;
    }
    if (this === functionProto) {
        return '';
    }
    var str = $functionToString(this);
    var match = $stringMatch(str, regex);
    var name = match && match[1];
    return name;
};