JavaScript keeps surprising me and this is another instance.(JavaScript让我感到惊讶,这是另一个例子。)
I just came across some code which I did not understood at first.(我刚刚遇到了一些我最初没有理解的代码。) So I debugged it and came to this finding:(所以我调试了它并得出了这个发现:)
alert('a'['toUpperCase']()); //alerts 'A'
Now this must be obvious if toUpperCase()
is defined as a member of string type, but it did not make sense to me initially.(现在,如果将toUpperCase()
定义为字符串类型的成员,这一点必须明显,但最初对我来说没有意义。)
Anyway,(无论如何,)
does this work because toUpperCase
is a member of 'a'?(这是否有效,因为toUpperCase
是'a'的成员?) Or there is something else going on behind the scenes?(或者幕后还有其他事情发生?)
the code I was reading has a function as follows:(我正在阅读的代码具有如下功能:) function callMethod(method) { return function (obj) { return obj[method](); //**how can I be sure method will always be a member of obj** } } var caps2 = map(['a', 'b', 'c'], callMethod('toUpperCase')); // ['A','B','C'] // ignoring details of map() function which essentially calls methods on every // element of the array and forms another array of result and returns it
It is kinda generic function to call ANY methods on ANY object.(它是在ANY对象上调用ANY方法的通用函数。) But does that mean the specified method will already be an implicit member of the specified object?(但这是否意味着指定的方法已经是指定对象的隐式成员?)
I am sure that I am missing some serious understanding of basic concept of JavaScript functions.(我确信我对JavaScript函数的基本概念缺乏认真的理解。) Please help me to understand this.(请帮我理解这个。)
ask by Mahesha999 translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…