在严格模式下使用 this.inherited(arguments) 时出现 DOJO 错误

DOJO error when using this.inherited(arguments) in strict mode

我正在为 Dijit 自定义小部件声明基础 "Class"。

当在'strict mode'例程中时this.inherited(arguments);正在调用,我收到此错误:

Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

我需要保留用法'strict mode'。

知道如何解决吗?

define([
    'dojo/_base/declare',
    'dojo/topic',
    'dojo/_base/lang'
], function (
    declare,
    topic,
    lang
    ) {
    'use strict';
    var attachTo = 'myPanels';
    return declare(null, {
        id: null,
        title: null,
        postCreate: function () {
            // ERROR HERE
            this.inherited(arguments);
            this.placeAt(attachTo);
        },
        constructor: function () {
        },
    });
});

注意:删除 'strict mode' 解决了问题,但在我的情况下不是一个选项,因为我需要使用 'strict mode'.

这是已知问题,Dojo 使用 arguments.callee 进行内省并确定继承的(更准确地说是下一个)方法。要解决此问题,您需要创建自己的 arguments 对象,然后指定 arguments.callee 属性 并将其传递给 Dojo 进行内省。唯一的问题是将函数传递给它自己,但它可以很容易地解决,例如使用这个助手,将它放在一个模块中,例如 override.js:

"use strict";
define([], function () {
    var slice = Array.prototype.slice;
    return function (method) {
        var proxy;

        /** @this target object */
        proxy = function () {
            var me = this;
            var inherited = (this.getInherited && this.getInherited({
                // emulating empty arguments
                callee: proxy,
                length: 0
            })) || function () {};

            return method.apply(me, [function () {
                return inherited.apply(me, arguments);
            }].concat(slice.apply(arguments)));
        };

        proxy.method = method;
        proxy.overrides = true;

        return proxy;
    };
});

现在你可以用它来调用继承的方法

define([
    'dojo/_base/declare',
    'dojo/topic',
    'dojo/_base/lang',
    './override'
], function (
    declare,
    topic,
    lang,
    override
    ) {
    'use strict';
    var attachTo = 'myPanels';
    return declare(null, {
        id: null,
        title: null,
        postCreate: override(function (inherited) {
            inherited(); // the inherited method 
            this.placeAt(attachTo);
        }),
        methodWithArgs : override(function(inherited, arg1, arg2)) {
            inherited(arg1, arg2);
            // pass all arguments to the inherited method
            // inherited.apply(null,Array.prototype.slice.call(arguments, 1));
        }),
        constructor: function () {
        },
    });
});

自 v1.13.0 起,Dojo 框架已解决此问题。

假设您使用的是 Dojo 1.13.0 或更新版本,要从严格模式文件调用 this.inherited,只需传递对调用函数的引用(使用命名函数表达式或 NFE ) 作为第一个参数。

所以你上面的代码应该是这样的:

define([
    'dojo/_base/declare',
    'dojo/topic',
    'dojo/_base/lang'
], function (
    declare,
    topic,
    lang
    ) {
    'use strict';
    var attachTo = 'myPanels';
    return declare(null, {
        id: null,
        title: null,
        postCreate: function postCreate() { //(1) add function name
            //(2) pass function reference as the first argument
            this.inherited(postCreate, arguments);
            this.placeAt(attachTo);
        },
        constructor: function () {
        },
    });
});

请注意,命名函数表达式 (NFE) 在 IE8 和更早版本中存在很多错误,因此如果您支持这些浏览器,请不要使用它。