簡體   English   中英

typeOf顯示錯誤的結果

[英]typeOf showing incorrect results

var name = 33;

    if (typeof (name) === 'string') {
        console.log('its a string');
    }
    else if (typeof (name) === 'number') {
        console.log('Number were entered here');

    }

從控制台給一個字符串? 怎么來的 ?? 謝謝。

在全局范圍內聲明的變量就像說window.variablename = value .. namewindow對象的標准屬性

沒有。 請參閱隨附的小提琴。

https://jsfiddle.net/chrislewispac/vxy6tt8t/

arr = ["test", 1, "3"];

for (i = 0; i < arr.length; i++) {

    if (typeof (arr[i]) === 'string') {
        console.log('its a string');
    } else if (typeof (arr[i]) === 'number') {
        console.log('Number was entered here');
    } 
}

要解決以下評論。 看到這個小提琴:

http://jsfiddle.net/chrislewispac/d1vhfe67/

console.log("original value without declaring var name " + window.name);
var name = 33;

if (typeof name === 'string') {
    console.log('its a string');
} else if (typeof name === 'number') {
    console.log('Number was entered here');
}

這是相關的。 我了解全局名稱空間,但重要的是要顯示typeOf實際上按預期工作。 標題為“ typeof給出錯誤的結果”。 那是不對的。 它給出了正確的結果。

此外,他當前問題中的代碼給出了正確的結果。 因此,如果以后沒有持久的示例/解釋的情況下訪問此問題的人感到非常困惑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM