I rewrite the project from es5 to es6 and use angular 1.6.1
I use classes es6 . I do like this

 function getArgs(func) { var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1]; return args.split(',').map(function(arg) { return arg.replace(/\/\*.*\*\//, '').trim(); }).filter(function(arg) { return arg; }); } function extendClassConstructor(object, parameters) { let r = [], i, keys = getArgs(object); for (i = 0; i < keys.length; i++) { r.push({ key: keys[i], value: parameters[i] }); } angular.forEach(r, function (value, key) { Object.defineProperty(object, key, { value: value }); }); } export default class DinnerController { /** @ngInject */ constructor(DinnerService, $location, $filter, $scope, DinnerFactory, PaginationFactory, $timeout, CurrencyFactory) { extendClassConstructor(this, arguments); console.log(this); } } 

But here's the problem, if I pass this to extendClassConstructor , then there will be an error
Cannot read property '1' of null
on this line
var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1];
And so in general this does not expand
What am i doing wrong

I have no desire to write like that

 this.DinnerService = DinnerService; this.$location = $location; this.$filter = $filter; this.$scope = $scope; this.DinnerFactory = DinnerFactory; this.PaginationFactory = PaginationFactory; this.$timeout = $timeout; this.CurrencyFactory = CurrencyFactory; 

As one of the solutions I came up with

 class NgController { constructor(child, parameters) { let keys = this.getArgs(child); let r = [], i; for (i = 0; i < keys.length; i++) { r.push({ key: keys[i], value: parameters[i] }); } angular.forEach(r, (obj) => { Object.defineProperty(this, obj.key, { value: obj.value }); }); } getArgs(func) { let args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1]; return args.split(',').map( (arg) => { return arg.replace(/\/\*.*\*\//, '').trim(); }).filter( (arg) => { return arg; }); } } 

And in the child class in the constructor, do so.

 class DinnerController extends NgController super(DinnerController, arguments); 

But again, is it possible to expand the prototype of the classes and just then call this.extendPropeties(arguments) in the constructor this.extendPropeties(arguments)
Type as possible?

  • the problem is that this you have in the constructor is not a function, it is a constructed object, therefore func.toString().match( returns null - Grundy
  • so far the best way out is as I suggested? - jashka
  • not. In fact, it is generally not clear what you are doing. In fact, you could just pass a constructor instead of an object. or as in your decision - the class itself. In addition, you could use the $ inject property. - Grundy
  • I don’t have a desire to write like that - in this case, it’s worth looking in the direction of typescript, there you can quickly create properties - Grundy

0