TypeError:无法读取未定义的属性“ searchContact”

时间:2019-07-18 19:06:45

标签: javascript class

我找不到问题,我看不到我未定义或拼写错误的任何内容,请帮助,我知道这里有一些不相关的代码,但是如果我将其遗漏,则您可能不明白我在尝试什么做我的代码,是的,我是初学者,所以我不知道任何高级方法,所以代码很长

class AddressBook{
    constructor(){
        this.myContact=[];
        this.myContact.push(new Contact("Homer", 98849959));
        this.myContact.push(new Contact("Marge", 84774744));
        this.myContact.push(new Contact("Lisa", 86994994));
        this.myContact.push(new Contact("Maggie", 94775883));
        this.myContact.push(new Contact("Bart", 88838848));
        var input = require("readline-sync");
        console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
        var choice = input.questionInt(">>> ");
        while(choice != 3){
            switch(choice){
                case 1:
                    this.showAllContacts();
                    console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
                    choice = input.questionInt(">>> ");
                    break;
                case 2:
                    search();
                    console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
                    choice = input.questionInt(">>> ");
                    break;
                default:
                    console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
                    choice = input.questionInt(">>> ");
            }
        console.log("Good bye!");
    }
    searchContact(nameFind){
        var found = false;
        for(var i = 0; i < this.myContact.length ; i++){
            if(this.myContact[i].name == nameFind){
                found = true;
                return(this.myContact[i]);
            }
        }
        if(found == false){
            return("not found!");
        }
    }
}

function search(){
    var input = require("readline-sync");
    var nameSearch = input.question("Enter name of the contact: ");
    var b = myAddressBook.searchContact(nameSearch);
    if(b != "not found!"){
        console.log(b.name+"'s phone number is "+b.mobileNumber);
    }
    else{
        console.log(b);
    }
}
var myAddressBook = new AddressBook();

我应该找回姓名和电话号码,但我却收到TypeError:无法读取未定义的属性'searchContact'

2 个答案:

答案 0 :(得分:0)

您要在search构造函数返回之前调用myAddressBook.searchContact(nameSearch)(由于调用AddressBook而导致错误的函数),这意味着此时myAddressBookundefined,因为new AddressBook()尚未返回。

要解决此问题,您可以将调用search的代码移到构造函数之外,这是一个示例:

class AddressBook{
    constructor(){
        this.myContact=[];
        this.myContact.push(new Contact("Homer", 98849959));
        this.myContact.push(new Contact("Marge", 84774744));
        this.myContact.push(new Contact("Lisa", 86994994));
        this.myContact.push(new Contact("Maggie", 94775883));
        this.myContact.push(new Contact("Bart", 88838848));
        var input = require("readline-sync");        
    }

    userInteraction () {
        console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
        var choice = input.questionInt(">>> ");
        while(choice != 3){
            switch(choice){
                case 1:
                    this.showAllContacts();
                    console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
                    choice = input.questionInt(">>> ");
                    break;
                case 2:
                    search();
                    console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
                    choice = input.questionInt(">>> ");
                    break;
                default:
                    console.log("Personal Addressbook\n----------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n");
                    choice = input.questionInt(">>> ");
            }
        console.log("Good bye!");
    }
    ....
}
....
var myAddressBook = new AddressBook();
myAddressBook.userInteraction();

答案 1 :(得分:0)

您有错字,需要更改此字词(暂时不公开):

        }
    console.log("Good bye!");
}
searchContact(nameFind){
    var found = false;
    for(var i = 0; i < this.myContact.length ; i++){

为此(括号中关闭):

        }
    }
    console.log("Good bye!");
}
searchContact(nameFind){
    var found = false;
    for(var i = 0; i < this.myContact.length ; i++){