2

Is there a way to check the instance of an object created by an AngularJS factory?

angular.module('so')
    .factory('UserFac', function () {
        return function (first, last) {
             return {
                 name : first + ' ' + last
             };
        }
    })
    .controller('UserCtrl', function (User) {
        var user = new UserFac('John', 'Doe');

        function isUser(userObj) {
            // your answer here...
        }

        if (isUser(user)) {
            // does not matter
        }
    });

Unfortunately I found no way to check the instance of the factory object by the usual JavaScript ways like:

user instanceof UserFac

or

typeof user === 'UserFac'

or

user.constructor === UserFac

or

user.prototype === UserFac

It looks like the internal AngularJS code for factories/services conceals the prototype/constructor property.

Websearch is quite painful, as (most of) all results deal with the difference between a service and a factory.

Thanks for your help!

2 Answers 2

3

One problem is that your constructor function is anonymous, another problem is that you're returning an object literal (which is not an instance of your constructor function, but an instance of Object constructor) from your constructor function.

instanceof will work properly if you do it as shown below:

angular.module('so', []);
angular.module('so')
  .factory('UserFac', function() {
    function UserFac(first, last) {
     //--------^-- give it the factory name
      this.name = first + ' ' + last
     //-^-- use the object created internally using this keyword
    }
    return UserFac;
  })
  .controller('UserCtrl', function(UserFac) {
    var user = new UserFac('John', 'Doe');
    console.log(user instanceof UserFac)
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Thats it! Thank you very much for your quick reply. Looks like the name of the constructor function is not necessarily needed and the main point was the usage of "this". The following code should do the trick, too: angular.module('so', []); angular.module('so') .factory('UserFac', function() { return (first, last) { this.name = first + ' ' + last; //-^-- use the object created internally using this keyword }; });
0

Agree with @T.J. Alternatively you can also implement as below.

You can read their documentation how actually Angular instantiate named providers. This is a best practice defined by Angular community itself.

Fiddle Link

Code:

angular.module('so', []);
angular.module('so')
  .factory('UserFac', function UserFac() {
   return function (first, last) {
      this.name = first + ' ' + last
    }
  })
  .controller('UserCtrl', function(UserFac) {
    var user = new UserFac('John', 'Doe');
    console.log(user instanceof UserFac)
    console.log("Name:"+user.name);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.