arguments.callee在es6中用于确定调用函数的替代方法

时间:2014-03-23 10:38:22

标签: javascript ecmascript-6 arguments

在框架中,我正在开发,我已经构建了机制,允许定义私有和受保护的属性和方法 我在ES5规范中发现的唯一能力就是使用arguments.callee
像这样:

descriptor.method = function () {
    if (__callerIsProptected(arguments.callee.caller.caller, cls))
        return value.apply(this, __defaults(_.values(arguments), defaults));
    throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
};

在严格模式下调用arguments.callee和arguments.caller导致异常抛出有没有方便的替代方法呢?

更新 - 添加了整个调用的功能代码

    function __descriptor(cls, type, name, descriptor, access) {
    //protected private non-function descriptor.value is replaced by get/set pair
    if (access !== 'public' && type == 'property') {
        delete descriptor.value;
        delete descriptor.writable;
        _.isFunction(descriptor.get) || (descriptor.get = function () {
            return this.__get(name);
        });
        _.isFunction(descriptor.set) || (descriptor.set = function (value) {
            return this.__set(name, value);
        });
    }
    //remove uselesses
    if (_.isFunction(descriptor.get) || _.isFunction(descriptor.set)) {
        delete descriptor.value;
        delete descriptor.writable;
        if (!_.isFunction(descriptor.get)) {
            descriptor.get = function () {
                return this.__get(name);
            };
        }
        if (!_.isFunction(descriptor.set)) {
            descriptor.set = function (value) {
                return this.__set(name, value);
            };
        }
    } else {
        delete descriptor.get;
        delete descriptor.set;
    }
    if (descriptor.get) {
        var getter = descriptor.get;
        //mutate getter and setter if given respectively to access level
        if (access === 'public') {
            descriptor.getter = function () {
                return getter.apply(this, arguments);
            };
        } else if (access === 'protected') {
            descriptor.getter = function () {
                if (__callerIsProptected(arguments.callee.caller.caller, cls))
                    return getter.apply(this, arguments);
                throw 'Attempt to get ' + access + ' property "' + cls._name + '::' + name + '"';
            };
        } else if (access === 'private') {
            descriptor.getter = function () {
                if (__callerIsPrivate(arguments.callee.caller.caller, cls))
                    return getter.apply(this, arguments);
                throw 'Attempt to get ' + access + ' property "' + cls._name + '::' + name + '"';
            };
        }
        descriptor.getter._class = cls;
    }
    if (descriptor.set) {
        var setter = descriptor.set;
        //mutate getter and setter if given respectively to access level
        if (access === 'public') {
            descriptor.setter = function () {
                return setter.apply(this, arguments);
            };
        } else if (access === 'protected') {
            descriptor.setter = function () {
                if (__callerIsProptected(arguments.callee.caller.caller, cls))
                    return setter.apply(this, arguments);
                throw 'Attempt to set ' + access + ' property "' + cls._name + '::' + name + '"';
            };
        } else if (access === 'private') {
            descriptor.setter = function () {
                if (__callerIsPrivate(arguments.callee.caller.caller, cls))
                    return setter.apply(this, arguments);
                throw 'Attempt to set ' + access + ' property "' + cls._name + '::' + name + '"';
            };
        }
        descriptor.setter._class = cls;
    }
    if (descriptor.value !== undefined) {
        if (!_.isFunction(descriptor.value)) return descriptor;
        var value = descriptor.value;
        var defaults = descriptor.defaults || [];
        if (access === 'public' && type == 'method') {
            descriptor.method = function () {
                return value.apply(this, __defaults(_.values(arguments), defaults));
            };
        } else if (access === 'protected') {
            descriptor.method = function () {
                if (__callerIsProptected(arguments.callee.caller.caller, cls))
                    return value.apply(this, __defaults(_.values(arguments), defaults));
                throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
            };
        } else if (access === 'private') {
            descriptor.method = function () {
                if (__callerIsPrivate(arguments.callee.caller.caller, cls))
                    return value.apply(this, __defaults(_.values(arguments), defaults));
                throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
            };
        }
        descriptor.method._class = cls;
    }
    return descriptor;
}

1 个答案:

答案 0 :(得分:0)

一旦我开发了相同的框架(已废弃),并且以严格模式找出调用者的唯一方法是实际上从堆栈跟踪中引发异常和RegExp调用者名称。据我记得,它并不总是精确的。例如看caller-id脚本的代码