Vi Veri Veniversum Vivus Vici
I've created mixin to dynamically add 'use strict' literal to the start of functions in classes.
First, we need to create cache of keys which contains in second parameter of Ext.define:
  1. (function () {
  2. var old = Ext4.define,
  3. del = [
  4. 'extend', 'mixins', 'statics', 'strictMethods',
  5. 'constructor', 'init', 'notStrictMethods', 'initComponent'
  6. ],
  7. res;
  8. // save keys in cache and delete potentially unsafe keys
  9. window.StrictCache = {};
  10. Ext4.define = function (name, data) {
  11. res = Ext4.Object.getKeys(data);
  12. if (data.statics) {
  13. if (Ext4.isArray(data.statics.notStrictMethods)) {
  14. del = Ext4.Array.merge(del, data.statics.notStrictMethods);
  15. }
  16. res = Ext4.Array.merge(res, Ext4.Object.getKeys(data.statics));
  17. }
  18. res = Ext4.Array.unique(res);
  19. if (Ext4.isArray(data.notStrictMethods)) {
  20. del = Ext4.Array.merge(del, data.notStrictMethods);
  21. }
  22. Ext4.each(del, function (toDelete) {
  23. Ext4.Array.remove(res, toDelete);
  24. });
  25. StrictCache[name] = res;
  26. return old.apply(this, arguments);
  27. };
  28. })();

Next, we must to write mixin:
  1. /**
  2.  * @class Lib.StrictMixin
  3.  * Mixin class for 'use strict'
  4.  * @author guyfawkes
  5.  * @docauthor guyfawkes
  6.  */
  7. Ext4.define('Lib.StrictMixin', {
  8. onClassMixedIn : function (mixedClass) {
  9. var fn;
  10. // mixedClass can be both class prototype or constructor (in this case we need to get function from its prototype)
  11. // please note we don't break loop when strictMethods were found: it must be both in 'statics' and non-static of ExtJS 4's class
  12. Ext4.each([mixedClass, mixedClass.prototype], function (obj, index) {
  13. fn = function(method) {
  14. if (Ext4.isFunction(obj[method])) {
  15. console && console.info(
  16. Ext4.String.format(
  17. '{0} found in {1} of {2}',
  18. method, (index ? 'constructor prototype' : 'prototype'), obj.$className
  19. )
  20. );
  21. obj[method] = new Function("'use strict';nreturn " + obj[method].toString())();
  22. }
  23. };
  24. // if we need to loop over all methods, get keys from cache (or we'll get functions from parent classes and got an error
  25. if (Ext4.isString(obj.strictMethods) && (obj.strictMethods === 'ALL') && Ext4.isString(obj.$className)) {
  26. Ext4.each(StrictCache[obj.$className], fn);
  27. }
  28. else {
  29. // if strictMethods is an array, loop over it
  30. if (Ext4.isArray(obj.strictMethods)) {
  31. Ext4.each(obj.strictMethods, fn);
  32. }
  33. }
  34. });
  35. }
  36. });


@темы: ExtJS 4, JavaScript, use strict