JavaScript enum sample - how does this work? - Stack Overflow

This bit of code is from this answer.I'm trying to understand how it works but I'm not gettin

This bit of code is from this answer.

I'm trying to understand how it works but I'm not getting it all.

What I think is happening is TEST_ERROR is a closure so ErrorValue can't be changed. One would reference a value like this: TEST_ERROR.SUCCESS. Please correct me if either of those statements are incorrect.

What I don't understand is what the return statement is doing. It's returning an object made up of different ErrorValues, but returning it to what? And what is it returning from? And when is it being 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!

Paul

This bit of code is from this answer.

I'm trying to understand how it works but I'm not getting it all.

What I think is happening is TEST_ERROR is a closure so ErrorValue can't be changed. One would reference a value like this: TEST_ERROR.SUCCESS. Please correct me if either of those statements are incorrect.

What I don't understand is what the return statement is doing. It's returning an object made up of different ErrorValues, but returning it to what? And what is it returning from? And when is it being 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!

Paul

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Dec 21, 2010 at 20:59 PaulPaul 20.1k15 gold badges82 silver badges99 bronze badges 1
  • The function is enclosed by parenthesis that are executed. Something like (function() { alert('Hello'); })();. The return value (the {}) is assigned to TEST_ERROR. – joksnet Commented Dec 21, 2010 at 21:04
Add a ment  | 

6 Answers 6

Reset to default 6

TEST_ERROR is a closure so ErrorValue can't be changed.

TEST_ERROR will end up just being the object specified in the return statement inside the anonymous function. This object can be changed.

One would reference a value like this: TEST_ERROR.SUCCESS

That's correct.

What I don't understand is what the return statement is doing. It's returning an object made up of different ErrorValues, but returning it to what? And what is it returning from? And when is it being called?

The return statement is returning from the anonymous function that's declared with

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

The () at the end means that the anonymous function is being called immediately after it is declared, and the value inside the return block is assigned to TEST_ERROR

Here's a good article on closures and emulating private variables that might be useful.

It is returning its result to TEST_ERROR

var TEST_ERROR = 

and it is being called immediately:

})();

This is a mon javascript pattern. You create an anonymous function just for the privacy/scoping it provides, then execute it immediately rather than keeping it around.

This code creates a class called TEST_ERROR. The function ErrorValue is the constructor of the class which defines two attributes: value and friendly. The class has two functions: toString (which returns friendly for any given instance) and valueOf (which returns the 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 that class (kind of like C#'s static members).

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 have be {SUCCESS: ..., FAIL: ..., ID_ERROR: ...} and you can't mess with the values them selfs since you have not access to ErrorValue.

Yes it is essentially returning an Object with three members SUCCESS, FAIL, ID_ERROR, each one representing an ErrorValue. In javascript we can access the members through dot notation or via indexer.

ErrorValue cannot be instantiated outside of the object because it's scope is limited...

I'm pretty (very) sure you can still change the values:

TEST_ERROR.FAIL.value = 7;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744516180a4578288.html

相关推荐

  • JavaScript enum sample - how does this work? - Stack Overflow

    This bit of code is from this answer.I'm trying to understand how it works but I'm not gettin

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信