在严格模式下使用this.inherited(arguments)时出现DOJO错误 [英] DOJO error when using this.inherited(arguments) in strict mode

查看:475
本文介绍了在严格模式下使用this.inherited(arguments)时出现DOJO错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当在'strict mode'例程 this.inherited(arguments);被调用,我收到这个错误:


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


我需要保留使用严格模式。



任何想法如何解决?

 
'dojo / _base / declare',
'dojo / topic',
'dojo / _base / lang'
],function(
declare ,
主题,
lang
){
'use strict';
var attachTo ='myPanels';
return declare(null,{
id:null,
title:null,
postCreate:function(){
// ERROR HERE
this.inherited(arguments);
this.placeAt (attachTo);
},
构造函数:function(){
},
});
});

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

解决方案

这是已知问题,Dojo使用 arguments.callee 进行自省,并确定继承的(更准确地说是下一个)方法。要解决这个问题,您需要创建自己的参数对象,然后指定 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({
//模拟空参数
callee:proxy,
length:0
}))|| function(){};

return method.apply (我,[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(); //继承的方法
this.placeAt(attachTo);
}),
methodWithArgs:override(function ,arg1,arg2)){
inherited(arg1,arg2);
//将所有参数传递给继承的方法
// inherited.apply(null,Array.prototype.slice.call (参数1));
}),
构造函数:function(){
},
});
});


I am declaring a base "Class" for a Dijit Custom widget.

When in 'strict mode' routine this.inherited(arguments); is being called, I receive this error:

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

I need to keep the usage 'strict mode'.

Any idea how to solve it?

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 () {
        },
    });
});

Notes: removing 'strict mode' solve the issue, but is not an option in my case as I need to use 'strict mode'.

解决方案

This is known issue, Dojo uses arguments.callee for introspection and to determine the inherited (more precisely the next) method. To workaround this issue you need to create your own arguments object then specify arguments.callee property and pass it to Dojo for introspection. The only one problem is to pass the function to itself, but it could be easily solved, for example with this helper, place it in a module, for example 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;
    };
});

now you can use it to call inherited methods

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 () {
        },
    });
});

这篇关于在严格模式下使用this.inherited(arguments)时出现DOJO错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆