Having worked with Angularjs for a few months now, I am curious about implementing efficient OOP within this framework.
Currently, I am involved in a project where I need to create instances of a "terminal" class that includes constructor properties and various methods. In regular JS, I typically use the pseudoclassical pattern to separate constructor properties from methods in the prototype chain.
Based on what I know about Angularjs, using model.service() seems like the best option as it allows me to create a new instance of the service each time it is called. However, the common implementation I have come across when defining methods looks like this:
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
};
});
But wouldn't this create the function sayHello() every time the class is called? I am contemplating whether it would be more effective to separate the functions. For example:
myApp.service('helloWorldFromService', function() {
this.msg= "Hello, World!";
});
helloWorldFromService.prototype.showMessage = function() {
return this.msg;
};
In this way, the showMessage() function would only be created once in memory and shared across all instances of the services created.
If it is indeed possible (and increases code efficiency), how would one go about implementing this? (Note: the code above is just a speculation)
Thank you