Could someone guide me how to set my own length for a String, as in "string".length or String("string").length.
Afaik, String is a descendant from Object, and the length assignment probably takes place in the prototype.constructor, as well as in the prototype.__defineSetter__;
Edit:
The motivation is to learn how far javascript in current Browsers can pushed in order to implement one’s own language in it. Operator overloading is my second great concern, which seems is intentionally blocked (i.e. no arguments passed to the operator function).
Some motivation comes from:
http://code.google.com/p/traceur-compiler/
UPDATE:
Addition to new Objects can be locked with:
Object.preventExtensions(String.prototype);
Object.seal(obj)…disallows configurability
Object.freeze(obj)…additionally disallows writing
see here: http://msdn.microsoft.com/en-us/library/ff806191(v=vs.94).aspx#Y240
I guess there may be some way do undo this locking states….
Redefinition could be done like this:
Object.defineProperty(String, "length", {
value: 101,
writable: true,
enumerable: true,
configurable: true
});
However will throw “redefine_disallowed” in Chrome V8.
The JS-engine code that throws the error can be found here under DefineOwnProperty:
http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/v8natives.js?r=8073
That is not quite possible. Theoretically, this would do the job:
But on Chrome it fails with the error
redefine_disallowed.I honestly think there is a better solution for your scenario.