Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Object doesn't support method 'freeze'

I am trying to create an enummeration in Javascript. The javascript code used is

var FeatureName = {
"FEATURE1": 1,
"FEATURE2": 2,
"FEATURE3": 3,
"FEATURE4": 4,
"FEATURE5": 5
}
Object.freeze(FeatureName);

When the method Object.freeze(FeatureName), is called it works fine for all the browsers except IE7 and IE8. Is there any alternative to this?

like image 378
Anubhav Ranjan Avatar asked Oct 29 '12 07:10

Anubhav Ranjan


1 Answers

John Resig provides an alternative. I haven't tried it in the browsers you mention. Try it and let us know.

http://ejohn.org/blog/ecmascript-5-objects-and-properties/

Object.freeze = function( obj ) {
  var props = Object.getOwnPropertyNames( obj );

  for ( var i = 0; i < props.length; i++ ) {
    var desc = Object.getOwnPropertyDescriptor( obj, props[i] );

    if ( "value" in desc ) {
      desc.writable = false;
    }

     desc.configurable = false;
     Object.defineProperty( obj, props[i], desc );
  }

  return Object.preventExtensions( obj );
};
like image 75
ColBeseder Avatar answered Oct 08 '22 01:10

ColBeseder