2014-07-10 19 views
0

我在網頁中看到了以下javascript。String [..]數組操作的用法

s = String["fromC" + aa.nodeValue]; 

我可以知道String[...]的用法是什麼?

+1

你從哪裏看到的?我需要一個鏈接,否則我無法相信。 – Bergi

回答

2

方括號表示法只是訪問對象屬性或方法的另一種方法,通常通過某種變量來訪問,而不是通過符號或關鍵字訪問屬性的點符號。

aa.nodeValue等於字符串「harCode」。這將這樣的工作

var s = String["fromCharCode"]; 

即相當於

var s = String.fromCharCode; 

所以s現在是一個指針上String靜態fromCharCode方法。

同樣,如果aa.nodeValue等於「odePoint」,你會被引用String.fromCodePoint

1

它只是String.fromCharCode(),寫在模糊化的方式,也可以是這樣的:

aa = document.createTextNode("harCode"); 
s = String["fromC" + aa.nodeValue]; 

這將終於變成String["fromCharCode"]這相當於String.fromCharCode