下面的聲明是什麼意思?我知道lines = []
是一個數組,但我不知道其他人。Javascript變量定義 - 澄清
實際的代碼是使用JavaScript XPCOM
var line = {}, lines = [], hasmore;
do {
hasmore = istream.readLine(line);
lines.push(line.value);
} while(hasmore);
下面的聲明是什麼意思?我知道lines = []
是一個數組,但我不知道其他人。Javascript變量定義 - 澄清
實際的代碼是使用JavaScript XPCOM
var line = {}, lines = [], hasmore;
do {
hasmore = istream.readLine(line);
lines.push(line.value);
} while(hasmore);
逐行讀取文件中的行它創建3個變量
var line = {}; // creates an object
var lines = []; // creates an array
var hasmore; // undefined
聲明3個變量(參見:Declaring Multiple Variables in JavaScript)。
var line = {} // creates an empty object literal
lines = [] // creates an empty array literal
hasmore // creates an empty undefined variable, which can hold any datatype
來吧,試試這個控制檯上:
var line = {}, lines = [], hasmore;
然後訪問它們,你會看到:
line is Object,
lines is an array
hasmore is undefined
記住,JavaScript不跟蹤變量類型在「編譯期時間。」 'X = []; X = 3; x =「hi」;'完全有效(儘管不推薦)。變量的類型取決於你如何使用它,而不是你如何聲明它。 –
[可變聲明與多個逗號分隔值是什麼意思(例如var a = b,c,d;)]的可能重複(http://stackoverflow.com/questions/11076750/what-does-variable-declaration-with -multiple-comma-separated-values-mean-egv) –
和[var myArray = \ [\],name ;?](http://stackoverflow.com/q/6232778/218196) –