Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding better Javascript OOP architecture [duplicate]

As i read through some examples of Angularjs' UI add-on, i've stumbled over some code that showed me that my knowdledge of Javascript is quite improvable:

The following is a class inside of an Angular provider:

function Dialog(opts) {

            var self = this, options = this.options = angular.extend({}, defaults, globalOptions, opts);
            this._open = false;

            this.backdropEl = createElement(options.backdropClass);
            if(options.backdropFade){
                // ...
            }

            this.handleLocationChange = function() {
                self.close();
            };

            // more functions
        }

Pretty straightforward. But outside of that class, there are prototype functions, e.g the above invoked close()

Dialog.prototype.open = function(templateUrl, controller){
            var self = this, options = this.options;

            // .. some code
        };

Now i do not understand why that function is declared as a prototype, but handleLocationChange inside the class itself.

How do i decide which method to choose?

The full gist can be found here

like image 370
user2422960 Avatar asked Mar 24 '23 10:03

user2422960


1 Answers

Consider these 2 cases:

Dialog.prototype.open = function...

Dialog.open = function....

First case - every object created by calling new Dialog() will have this open function

Second case has nothing to do with dialog objects, consider it as static function.

EDIT

found a great answer here : javascript-class-method-vs-class-prototype-method

like image 181
Tala Avatar answered Apr 07 '23 06:04

Tala