前提条件:正确的 charCodeAt(i : Int) 性能应该是什么样子的:
关于这个问题:当范围没有用`this.x`指定而只用`x`来表示时-在这个例子中,由于JS解释器的范围解析,会导致奇怪的行为吗?是否有人遇到过类似的范围冲突的问题?
"test".charCodeAt(0)
116
"test".charCodeAt(1)
101
"test".charCodeAt(2)
115
"test".charCodeAt(3)
116
"test".charCodeAt(4)
NaN
使用call或apply时会发生什么:
>"test".charCodeAt.apply(this, [0,1,2,3])
91
//that's fine, except for 91!
"test".charCodeAt.call(this,0)
91
"test".charCodeAt.call(this,4)
101
"test".charCodeAt.call(this,5)
99
"test".charCodeAt.call(this,50)
NaN
"test".charCodeAt.call(this,6)
116
"test".charCodeAt.call(this,8)
68
x = "test".charCodeAt
function charCodeAt() { [native code] }
x.call(x,0)
102
参数被正确传递。关键在于使用call或apply的第一个参数所传递的作用域,并且改变了this指针的valueOf。
我不太确定发生了什么,而且无法在新的控制台窗口(Chrome v15)中重现这种行为:
x = "test"
"test"
"".charCodeAt.call(this.x,0)
116
OK.
关于这个问题:当范围没有用`this.x`指定而只用`x`来表示时-在这个例子中,由于JS解释器的范围解析,会导致奇怪的行为吗?是否有人遇到过类似的范围冲突的问题?
String
来说是安全的,但请永远不要尝试对Array
或Object
进行扩展。 - ThiefMaster