2015-10-19 2 views
3

Я объявляю базовый «класс» для виджета Dijit Custom.Ошибка DOJO при использовании this.inherited (arguments) в строгом режиме

Когда в '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

Мне нужно сохранить режим «строгого режима».

Любая идея, как ее решить?

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'.

+2

См http://dojo-toolkit.33424.n3.nabble.com/use-strict-and-this-inherited-td3990942.html – Xodrow

ответ

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; 
    }; 
}); 

теперь вы можете использовать его для вызова наследуется методы

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() { 
     }, 
    }); 
}); 
Смежные вопросы