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 errorCannot read property '1' of null
on this linevar 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?
thisyou have in the constructor is not a function, it is a constructed object, thereforefunc.toString().match(returnsnull- Grundy