有人可以指导我如何为 String
设置自己的长度,如 "string".length
或 String("string").length
>。
Afaik,String
是 Object
的后代,长度分配可能发生在 prototype.constructor
以及 >prototype.__defineSetter__;
编辑:
动机是了解当前浏览器中的 javascript 可以发展到什么程度,以便在其中实现自己的语言。运算符重载是我第二个最关心的问题,它似乎是故意阻止的(即没有参数传递给运算符函数)。
一些动机来自:
http://code.google.com/p/traceur-compiler/< /a>
更新:
添加到新对象可以通过以下方式锁定:
Object.preventExtensions(String.prototype);
Object.seal(obj)...不允许可配置性
Object.freeze(obj)...另外不允许写入,
请参见此处:http://msdn.microsoft.com/en-us/library/ff806191(v=vs.94).aspx#Y240
我想可能有某种方法可以撤消此锁定状态....
重新定义可以这样完成:
Object.defineProperty(String, "length", {
value: 101,
writable: true,
enumerable: true,
configurable: true
});
但是在 Chrome V8 中会抛出“redefine_disallowed”。
引发错误的 JS 引擎代码可以在 DefineOwnProperty 下找到:
http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/v8natives.js?r=8073
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
发布评论
评论(1)
这不太可能。理论上,这可以完成这项工作:
但在 Chrome 上,它会失败并出现错误
redefine_disallowed
。老实说,我认为对于您的情况有更好的解决方案。
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.