Correct way to create classes in JavaScript?

I am new to JavaScript and trying to figure out how I should write classes (my background is in the “regular” OO languages ​​like java and C ++).

I understand that I have two options:

  • If I want my class to have private methods and members, I cannot define them in a prototype. But in this case, they will be created for each new object created (memory problem).

  • If I define methods in the prototype of the class, I will not have encapsulation (this is strange for me as a java / C ++ developer: P).

Which of the two methods do you use? why?

+6
source share
3 answers

So, I don’t think there is a “right answer” to this question ... this is basically what you prefer and consider the best for your specific use. Many of my classes are “Static Classes,” for example.

var MyClassName = { methodName: function() { }, //... } 

Because I never need to create them. When I need to instantiate multiple instances, I use the prototype method.

If you need private variables, you can define a function / class to execute private variables and methods that should access those private vars inside this function / class. Then use the prototype method for all methods that do not need access to private vars. For instance.

 var PageClass = function() { var _birthdate; this.getBirthdate = function() { return typeof(_birthdate) == "undefined" ? null : _birthdate; } this.setBirthdate = function( date ) { if( typeof(date) == 'object' && date.constructor == Date ) { _birthdate = date; } else { throw "Invalid Argument Exception: PageClass.setBirthdate expects parameter of type 'Date'"; } } } PageClass.prototype.doSomething = function() { alert("DOING SOMETHING"); } 

Doing both should allow you to keep your instance a little lighter, but still give you some encapsulation. Until now, I have never worried about private vars.

+7
source

If you use a prototype structure, you would be better off using a way to implement classes and inheritance. You probably meant this article.

Usually I don’t think private members are used in javascript. ( edit ): not in instance classes. I regularly see some "modules" in our code base that have a personal state, but can be considered as single ones)

Kevin answers pretty much sums it up. It is technically possible to do without language encapsulation, but it is expensive if your class will be created a lot. I also think that you will need work to get protected visibility if you intend to use inheritance.

I don’t think I saw any personal things when I looked, for example, at external sources. They do "// private" on methods that should be private, which is a hint that they cannot be called directly. This means that you need more “discipline” to write getters / setters if you are going to code this way.

0
source

Why yes, there is ... now.

The answers above are true for their time.

Here is a new solution:

 'use strict'; // Declare our class class Metadata { // Declare the constructor and pass it some values - a and b are "defaults" constructor( ninfo, a = 1.0, b = 1.0 ) { // Set our class variables this.ninfo = ninfo; this.a = a; this.b = b; // Define our "secret" or nonenumerable options Object.defineProperty( this, 'addA', { enumerable: false, writable: false, // Have it add our passed in value value: n => this.a += n } ); } } // Call our class and pass in some variables let x = new Metadata( "we have a and b", 1.0 ); // Log our result if we call "addA" console.log( x.addA( 3 ) ); // => 4 // Log our object console.log( x ); // => Metadata { ninfo: 'we have a and b', a: 4, b: 2 } // Log our object 'for real' console.log( require( 'util' ).inspect( x, { showHidden: true, depth: null } ) ); // result // Metadata { // ninfo: 'we have a and b', // a: 4, // b: 2, // [addA]: { [Function: value] [length]: 1, [name]: 'value' } } 
0
source

Source: https://habr.com/ru/post/888043/


All Articles