AngularJS utility class - Good (right) or bad way?

I have read many articles on the Internet about AngularJS and JS in general and cannot find a better way to make a service (like a class) right.

Is this a good or bad way to do this (it works) - are the pros and cons?

.service('Field', function() {

  /**
   * Constructor
   */
  function Field(id, name) {
    this.id = id;
    this.name = name;
  }

  /**
   * Public method
   */
  Field.prototype.getName = function() {
    return this.name;
  }

  /**
   * Public method
   */
  Field.prototype.setName = function(name) {
    this.name = name;
  }

  return Field;
})


.controller('CreateCtrl', function($scope, Field) {

  $scope.fields = [
    new Field(1, "Test1"),
    new Field(2, "Test2")
  ];

  console.log($scope.fields[0].getName());
})

I was inspired by this article - https://medium.com/opinionated-angularjs/angular-model-objects-with-javascript-classes-2e6a067c73bc

+4
source share

All Articles