Subj : Re: How do you make an object's property const? To : Celso Aguiar From : Brendan Eich Date : Mon May 10 2004 07:05 pm Celso Aguiar wrote: > I've been trying to figure out how to make an object's > property constant from JavaScript, and have not found a > way, at least within SpiderMonkey latest 1.5. > > const O = function () { > this.p = 1; // can't do "const this.p = 1", syntax error. > } > const o = new O(); > o.p = 2; // I still can replace o.p > > A browse through Ecma standards was not conclusive. It should conclusively show that there's no way to do it in Edition 3, which doesn't have const at all. Edition 4's support for const allows you to declare a const instance member (see http://www.mozilla.org/js/language/js20/core/variables.html). But E4 is not done yet, and not implemented hereabouts. > Is this a SpiderMonkey bug (or missing implementation)? It's not a bug. Adding the feature as a random enhancement doesn't make sense either. Better to implement all of JS2, which I'm finally making time for. > Or is this a case where my only option to const an > object's property is from C/C++ via JSPROP_READONLY? That's the only way today. You might hack a makeConst native helper function that anyone can call to make a non-const property const. I'm not sure that an evil script in a mixed-trust setting could not abuse it, but I can't think of an attack right now. /be .