oop - Javascript prototype constant declaration -


i working restful api, , javascript code making rest queries via jquery's $.ajax() call.

i have implemented javascript rest class, show below (greatly simplified):

var rest = function (baseurlpath, errormessagehandler) {         ...     };  // declare http response codes constants rest.prototype.status_ok = 200; rest.prototype.status_bad_request = 400;  ... // other rest methods   rest.prototype.post = function (params) {         $.ajax({             type: 'post',             url: params.url,             data: params.data,             datatype: 'json',             contenttype: 'application/json; charset=utf-8',             beforesend: this._authorize,             success: params.success,             error: params.error || this._getajaxerrorhandler(params.errormessage)         });         };  ... // more rest methods  rest.prototype.executescenario = function (scenarioref) {         var self = this;          this.post({             url: 'myurlgoeshere',             data: 'mydatagoeshere',             success: function (data, textstatus, xhr) {                 if (xhr.status == 200) {                     console.log("everything went ok");                 }             },             error: function (xhr, textstatus, errormsg) {                 // todo: constants                 if (404 == xhr.status) {                     self.errormessagehandler("the scenario not exist or not queued");                 } else if (403 == xhr.status) {                     self.errormessagehandler("you not allowed execute scenario: " + scenarioref.displayname);                 } else if(423 == xhr.status) {                     self.errormessagehandler("scenario: " + scenarioref.displayname +  " in queue");                 }             }         });     }; 

the code works intended, have decided add constants beautify code , improve readability. have example several places in code checking xhr.status == 200 or xhr.status == 400 , on.

i can declare class variables rest.prototype.status_ok = 200;

but variable editable, , cannot think of how make them constant. in code example can this.status_ok = 123; , modify variable. have played around const keyword, no luck.

i have seen this: where declare class constants?, not help.

can point me in right direction how make these fields constant literal instead of variable?

using ecmascript 5's object.defineproperty can make value un-settable:

object.defineproperty(rest, "status_ok", {   enumerable: false,   // optional; if care enumerated keys   configurable: false,   writable: false,   value: 200 }); 

or, since default values, do:

object.defineproperty(rest, "status_ok", { value: 200 }); 

this makes rest.status_ok yield 200 when accessed, not respond attempts redefine or delete it. furthermore, configurable: false prevent attempt redefine property subsequent defineproperty call.

however, doesn't work in older browsers don't support es5's defineproperty (notably ie8 , below).


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -