Javascript apply 或 call 在 charCodeAt 上使用
前提:正确的 charCodeAt(i : Int) 性能会是什么样子:
"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 指定范围而仅 < em>x - 在此示例中,由于 JS 解释器的作用域解析,是否会导致奇怪的行为? 有人遇到过具有奇怪范围冲突的类似案例吗?
The premise: what the correct charCodeAt(i : Int) performance would look like:
"test".charCodeAt(0)
116
"test".charCodeAt(1)
101
"test".charCodeAt(2)
115
"test".charCodeAt(3)
116
"test".charCodeAt(4)
NaN
and here what happens when when using call or 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
The arguments are passed correctly. It is about the scope of which is passed with the first argument of call or apply and changes the valueOf from the this pointer.
I am not really sure what happened, and couldn't reproduce the behaviour in a new console window (Chrome v15):
x = "test"
"test"
"".charCodeAt.call(this.x,0)
116
OK.
As to the question: When the scope is not specified with this.x but only x -in this exampl-e, can odd behaviour result owing to the scope-resolution of the JS interpreter?
Did someone encounter similar cases with strange scope-conflicts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在所有这些调用中,例如:
this
的值可能是window
,而不是任何字符串值。试试这个:如果你为
this
传递一些虚假的东西,你就不能指望它能正常工作:-) 当你定义一个变量“x”然后引用“this. x”再次表示this
是window
,因此“this.x”与“window.x”相同,这将为您提供变量。In all those calls like:
the value of
this
is likely to bewindow
, not any string value. Try this:If you pass in something bogus for
this
, you can't expect it to work properly :-) The reason it works when you define a variable "x" and then refer to "this.x" is, again, thatthis
iswindow
, so "this.x" is the same as "window.x", which will get you your variable.this
在您的情况下是window
并且window.toString() === "[object Window]"
。所以你正在使用该字符串。
如果您希望 charCodeAt 函数更好地使用 var foo = String.prototype.charCodeAt; ,然后在某个字符串上调用它: foo.call('meow', 0)
this
iswindow
in your case andwindow.toString() === "[object Window]"
.So you are working with that string.
If you want the
charCodeAt
function better usevar foo = String.prototype.charCodeAt;
and then call it on some string:foo.call('meow', 0)