JavaScript enum sample - how does it work?

This bit of code is in this answer .

I am trying to understand how this works, but I am not getting it all.

I think this TEST_ERROR is a close, so ErrorValue cannot be changed. One could refer to such a value: TEST_ERROR.SUCCESS. Please correct me if any of these statements is incorrect.

I do not understand what the return statement does. It returns an object consisting of different ErrorValues, but returns it to what? And what is he coming back from? And when is it called?

var TEST_ERROR = (function() { function ErrorValue(value, friendly) { this.value = value; this.friendly = friendly; } ErrorValue.prototype = { toString: function() { return this.friendly; }, valueOf: function() { return this.value; } }; return { 'SUCCESS': new ErrorValue(0, 'Success'), 'FAIL': new ErrorValue(1, 'Fail'), 'ID_ERROR': new ErrorValue(2, 'ID error') }; })(); 

Thanks!

Floor

+4
source share
6 answers

TEST_ERROR is a closure, so ErrorValue cannot be changed.

TEST_ERROR will simply be the object specified in the return statement inside the anonymous function. This object can be changed.

You can reference this value: TEST_ERROR.SUCCESS

It is right.

I do not understand what the return expression does. This is returning an object consisting of different ErrorValues, but returning it to what? And what does he return? of? And when is it called?

The return statement is returned from an anonymous function declared using

(function() { ...})();

() at the end means that the anonymous function is called immediately after its declaration, and the value inside the return block is assigned TEST_ERROR

Here's a good article on closing and emulating private variables that may be useful.

+6
source

It returns its result TEST_ERROR

 var TEST_ERROR = 

and it is called immediately:

 })(); 

This is a regular javascript template. You create an anonymous function only to ensure confidentiality / define it, and then execute it immediately, and not save it.

+2
source

This code creates a class called TEST_ERROR . The ErrorValue function is a class constructor that defines two attributes: value and friendly . The class has two functions: toString (which returns friendly for any given instance) and valueOf (which returns value for any given instance). Finally, this class declares three class level attributes ( SUCESS , FAIL and ID_ERROR ) that can be accessed without an instance of this class (like C # static members).

+1
source
 var TEST_ERROR = ( // these parenthesis evaluate the function expression function() { // anonymous function function ErrorValue(value, friendly) { this.value = value; this.friendly = friendly; } ErrorValue.prototype = { toString: function() { return this.friendly; }, valueOf: function() { return this.value; } }; // return an object from the function return { 'SUCCESS': new ErrorValue(0, 'Success'), 'FAIL': new ErrorValue(1, 'Fail'), 'ID_ERROR': new ErrorValue(2, 'ID error') }; } ) (); // call the evaluated function which returns the "enum" object and assign // that value to TEST_ERROR 

So, yes TEST_ERROR will be {SUCCESS: ..., FAIL: ..., ID_ERROR: ...} , and you cannot communicate with its values, since you do not have access to ErrorValue .

0
source

Yes, this essentially returns an object with three members SUCCESS, FAIL, ID_ERROR, each of which represents an ErrorValue. In javascript, we can access the elements through dot notation or using an indexer.

ErrorValue cannot be created outside the object because its scope is limited ...

0
source

I am pretty (very) sure that you can still change the values:

 TEST_ERROR.FAIL.value = 7; 
0
source

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


All Articles