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

Gib*_*boK 5 javascript dojo

我正在为Dijit Custom小部件声明一个基础"Class".

'strict mode'常规this.inherited(arguments); 正在调用,我收到此错误:

未捕获的TypeError:严格模式函数或调用它们的参数对象可能无法访问'caller','callee'和'arguments'属性

我需要保持使用'严格模式'.

知道怎么解决吗?

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 () {
        },
    });
});
Run Code Online (Sandbox Code Playgroud)

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

cin*_*cin 1

这是已知问题,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;
    };
});
Run Code Online (Sandbox Code Playgroud)

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

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 () {
        },
    });
});
Run Code Online (Sandbox Code Playgroud)