In my opinion, the best way to create classes in JavaScript is to not do it. Forgive me for being stupid, but when working with JavaScript, try to forget about classes - they are not here, and you will agree that the language only concerns objects.
Without classes in the language, this means that less code needs to be written. In typical applications, most objects do not have siblings. You will have only one document , one window , one userList , etc. Create these objects using object literal notation:
var userList = { users: [] };
While there are no classes in JavaScript, there are constructors and prototypes. These concepts come in handy when you have several similar objects (for example, users contained in userList ). Your code example uses both of these concepts. Using names like myclass , it's hard to say what you're trying to model. Here is an example of the User constructor and an extension of its prototype:
var User = function (name) { this.name = name; }; User.prototype.sayHello = function () { return "Hello, my name is " + this.name; };
Jørn schou-rode
source share