使用JavaScript时“未定义”某些内容是什么意思?

时间:2013-11-02 17:36:06

标签: javascript

最近,我一直在学习JavaScript。我遇到了一些JavaScript错误,说“ _ _未定义” - 这究竟是什么意思,为什么会出现这种情况?我或多或少地想要解释为什么会出现这种错误,以及可以采取哪些措施来解决这个错误,或者为什么它通常首先发生。

例如:这里有两个函数(validate和onSearch)---当我尝试运行“onSearch”时,我在控制台中获得了Ran SEARCH ...跟踪,但它消失了。另外,当我通过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)功能

3 个答案:

答案 0 :(得分:2)

如果你得到一个TypeError&#34; Blah未定义&#34;或者&#34;无法读取foo&#34;的属性undefined,这意味着您拥有一个具有 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中的#34;松散&#34;模式),JavaScript会有非常令人惊讶的行为:它创建了一个全局变量。我称之为The Horror of Implicit Globals。这意味着如果不是上面的console.log(missing);,我就这样做了:

missing = "foo";

...我正在创建一个新的全局变量,即使该代码在函数内。值得庆幸的是,在ES5中,我们可以使用&#34; strict&#34;模式,这使它始终应该是ReferenceError。 : - )

答案 1 :(得分:0)

这通常意味着您要求的“事物”不存在(或请求它的函数找不到至少)。这可以是变量,对象或函数。

对于你所谈论的onSearch的情况,很可能找不到该功能。可能是文件中有函数的文件在文件请求后加载(所以onSearch在b.js中,请求它的人在a.js. a.js在你的{{1在b.js之前)。因此它尚未出现,因为javascript文件加载线性。

答案 2 :(得分:0)

您的问题

问题不在于onSearch未定义,而是使用未定义的变量db

<强>例

(从现在开始,我将假设qwertyuiopasddsghjdsvjkfhjkl未被宣布)

在以下情况下您会看到未定义的错误:

  • 您使用未声明的变量。

    qwertyuiopasddsghjdsvjkfhjkl; // ReferenceError: qwertyuiopasddsghjdsvjkfhjkl is not defined
    
  • 您在声明但未定义的变量上使用属性:     var a;     A·B; // TypeError:a未定义

在以下情况下未定义变量:

  • (错误)您尚未声明

    // 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已声明,但未定义

    如果声明falsemyVar未定义

    ,则会返回myVar

  • myVar === void(0)

    如果声明true并且myVar未定义,则返回myVar

    如果声明falsemyVar未定义

    ,则返回myVar

    如果未声明myVar

  • ,则会引发错误
  • myVar === undefined

    如果myVar === void(0)未被修改,则与undefined相同。

  • !myVarif(myVar)

    如果声明true并且

    ,则返回myVar
    • myVar未定义
    • myVar是假的(null0false''

    如果声明false并且myVar是真实的

    ,则返回myVar

    如果未声明myVar

  • ,则会引发错误
相关问题