2013-11-02 32 views
0

最近,我一直在學習JavaScript。我遇到一些JavaScript錯誤,說「_ _未定義」 - 這意味着什麼,爲什麼這會出現?我對或多或少的解釋了爲什麼會出現這種錯誤,以及可以採取什麼措施來解決這個問題,或者爲什麼它通常首先發生。當使用JavaScript時,某些東西是「未定義」時,這意味着什麼?

例如:這裏有兩個函數(validate和onSearch)---當我嘗試運行「onSearch」時,我在控制檯中獲得了Ran SEARCH ... trace,但是它消失了。另外,當我通過JSHero(試圖調試)運行它時,它告訴我「onSearch」是未定義的,我很好奇爲什麼。

我已經有一些使用ActionScript進行開發的經驗,但我完全不熟悉JavaScript。我真的很感謝你的意見和解釋,這實際上意味着什麼。謝謝。

function validate(query){ 
    console.log("Ran VALIDATE..."); 
    // Trim whitespace from start and end of search query 
    while(query.charAt(0) === " "){ 
     query = query.substring(1, query.length); 
    }; 

    while(query.charAt(query.length-1) === ""){ 
     query = query.substring(0, query.length-1); 
    }; 

    console.log("Query length:",query.length); 
    console.log("Beginning conditional..."); 
    // Check search length, must have 3 characters 
    if(query.length < 3){ 
     console.log("Display alert..."); 
     alert("Your search query is too small, try again."); 

     // (DO NOT FIX THE LINE DIRECTLY BELOW) 
     searchInput.focus(); 
    }else{ 
     console.log("Searching query..."); 
     onSearch(query); 
    }; 
}; 

// Finds search matches 
function onSearch(query){ 
//var search = function(query){ 

    console.log("Ran SEARCH..."); 
    // split the user's search query string into an array 
    var queryArray = query.join(" "); 

    // array to store matched results from database.js 
    var results = []; 

    // loop through each index of db array 
    for(var i=0, j=db.length; i<j; i++){ 

     // each db[i] is a single video item, each title ends with a pipe "|" 
     // save a lowercase variable of the video title 
     var dbTitleEnd = db[i].indexOf('|'); 
     var dbitem = db[i].tolowercase().substring(0, dbTitleEnd); 

     // loop through the user's search query words 
     // save a lowercase variable of the search keyword 
     for(var ii=0, jj=queryArray.length; ii<jj; ii++){ 
      var qitem = queryArray[ii].tolowercase(); 

      // is the keyword anywhere in the video title? 
      // If a match is found, push full db[i] into results array 
      var compare = dbitem.indexOf(qitem); 
      console.log("Compare:",compare); 
      if(compare != -1){ 
       results.push(db[i]); 
      }; 
     }; 
    }; 

    console.log("Hello"); 
    results.sort(); 

    // Check that matches were found, and run output functions 
    if(results.length === 0){ 
     noMatch(); 
    }else{ 
     showMatches(results); 
    }; 
}; 

編輯** 「DB」 在外部文件中定義。它只是一個URL數組。它仍然說它沒有被很好地定義,這就是我所要求的。

你如何定義 1)可變 2)功能

回答

2

如果你相處線TypeErrorBlah未定義」或「不能讀取屬性的undefinedfoo」,這意味着你有一個變量或屬性具有undefined,這是默認值一個變量,直到你給它分配一些東西。

這與您尚未定義並試圖讀取其值的變量相反,該變量將代替觸發ReferenceError

例如,考慮以下:

var foo; 
console.log(foo.bar); // "TypeError: Cannot read property 'bar' of undefined" 

foo變量存在,但其價值是undefined,所以試圖讀取屬性從它導致錯誤。

對比度,爲:

console.log(missing); // "ReferenceError: missing is not defined" 

這裏,符號missing沒有定義;引擎不知道你在說什麼。這通常表示缺少var聲明。如果你分配給一個你從未聲明過的變量(在ES3或ES5中處於「鬆散」模式):它創建一個全局變量。我稱之爲The Horror of Implicit Globals。這意味着,如果上述替代console.log(missing);,我這樣做:

missing = "foo"; 

...我會創建一個新的全局變量,即使該代碼是一個函數內。謝天謝地,在ES5中,我們可以使用「嚴格」模式,這使得它始終應該是這樣的。 :-)

+0

我仍然不是在下面這將引發一個錯誤。我會編輯我的帖子以獲得更準確的描述。 – Lindsay

0

這通常意味着「東西」你的要求不存在(或至少不能要求它的功能被發現)。這可以是一個變量,對象或函數。

對於onSearch,您所談論的是,最有可能找不到該功能。它可能是文件中的函數在文件請求後加載的(所以onSearch位於b.js中,請求的文件位於a.js.a.js位於b.js之前的<head>中)。因此它還不存在,因爲JavaScript文件加載線性。

0

你的問題

的問題不在於onSearch是不確定的,但它使用可變db這是不確定的。

  • 您使用的變量,你還沒有宣佈:

    案件

    (從現在起,我將承擔qwertyuiopasddsghjdsvjkfhjkl不宣)

    當你看到不確定的錯誤。

    qwertyuiopasddsghjdsvjkfhjkl; // ReferenceError: qwertyuiopasddsghjdsvjkfhjkl is not defined 
    
  • 您使用上聲明,但未定義的變量屬性: 風險價值; a。b; //類型錯誤:一個是未定義

變量是未定義時:

  • (錯誤)你還沒有宣佈它

    // qwertyuiopasddsghjdsvjkfhjkl is undefined 
    qwertyuiopasddsghjdsvjkfhjkl; // ReferenceError: qwertyuiopasddsghjdsvjkfhjkl is not defined 
    
  • (沒有錯誤)您已聲明但它沒有價值

    var a; //a is undefined 
    
  • (沒有錯誤)分配給void(0)變量(你可以改變0的一切),或與未改性undefined

    var a = undefined; //a is undefined 
    var a = void(0); //a is undefined 
    undefined = 'abc'; 
    var a = undefined; //a is NOT undefined 
    

如何檢查

如果你不知道變量未定義,您可以使用

  • typeof myVar === 'undefined'

    它返回true如果:

    • myVar沒有聲明
    • myVar聲明,但未定義

    它返回false如果myVar聲明並myVar不是未定義

  • myVar === void(0)

    它返回true如果myVar聲明和myVar未定義

    它返回false如果myVar聲明並myVar不是未定義

    這將引發myVar沒有聲明錯誤

  • myVar === undefined

    這是如果undefined尚未修改,則與myVar === void(0)相同。

  • !myVarif(myVar)

    它返回true如果myVar被聲明和

    • myVar未定義
    • myVar是falsy(null0false''

    它返回false如果myVar聲明和myVar是truthy

    如果myVar沒有聲明

相關問題