为什么函数没有返回任何值?它返回[对象对象]或未定义

时间:2016-10-15 19:06:21

标签: jquery

我正在学习JQuery,我正在试图找出以下函数为什么没有返回任何内容。

这是脚本:

function Person(name,country){
    this.name = name;
    this.country = country;
}
var addressBook = {
  contact: new Array(),
  totalContact: 0,
  newContact: "",
  addContact: function(newName, newCountry){
    this.totalContact += 1;
    this.newContact = new Person(newName, newCountry);
    this.contact[this.totalContact.length] = this.newContact;
  },
  getContactList1: function(){
    for(var key in this.contact){
      return key;
    }
  },
  getContactList2: function(){
    for(var key in this.contact){
        return this.contact[key];
    }
  },
  getContactList3: function(){
    for(var i = 0; i< this.totalContact.length; i++){
		
      if(this.contact[i]==="undefined"){
        return "What's wrong here?";
      }else{
        return this.contact[i];
      };
	}
  }
};
addressBook.addContact("HarryPotter","Philippines");
$('.addressBook').append("<div class='test'>Why is getContactList1 not returning the keys: <br /> "+addressBook.getContactList1()+"</div>");
addressBook.addContact("Melody","Philippines");
$('.addressBook').append("<div class='test'> What is causing this in getContactList2:<br /> "+addressBook.getContactList2()+"</div>");
$('.addressBook').append("<div class='test'> What is causing this in getContactList3:<br />"+addressBook.getContactList3()+"</div>");
.addressBook{
}
.test{
  margin-bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="addressBook"></div>

而是提供反馈:

[object Object]

undefined

我将非常感谢你的帮助:)。

1 个答案:

答案 0 :(得分:0)

在第一个和第三个方法变体中,您将Person对象连接到带有+的字符串。由于您没有指定如何将此类对象转换为字符串,因此它显示为[object Object],这是将任何普通对象转换为字符串的默认方式。

您可以通过为Person对象提供toString方法来解决此问题。

第二种和第三种方法变体也存在问题,因为它们使用this.totalContact.length,但应该是this.contact.lengthtotalContact是一个没有length的数字} property。。

所以这里有一些工作代码(我没有改变你的输出文本,所以它现在没有意义;-)):

function Person(name,country){
    this.name = name;
    this.country = country;
    this.toString = function() { // added this function
        return JSON.stringify(this);        
    }
}

var addressBook = {
  contact: new Array(),
  totalContact: 0,
  newContact: "",
  addContact: function(newName, newCountry){
    this.totalContact += 1;
    this.newContact = new Person(newName, newCountry);
    this.contact[this.contact.length] = this.newContact; // error fixed.
  },
  getContactList1: function(){
    for(var key in this.contact){
      return key;
    }
  },
  getContactList2: function(){
    for(var key in this.contact){
        return this.contact[key];
    }
  },
  getContactList3: function(){
    for(var i = 0; i< this.contact.length; i++){ // error fixed.
      if(this.contact[i]==="undefined"){
        return "What's wrong here?";
      }else{
        return this.contact[i];
      };
	}
  }
};
addressBook.addContact("HarryPotter","Philippines");
$('.addressBook').append("<div class='test'>Why is getContactList1 not returning the keys: <br /> "+addressBook.getContactList1()+"</div>");
addressBook.addContact("Melody","Philippines");
$('.addressBook').append("<div class='test'> What is causing this in getContactList2:<br /> "+
addressBook.getContactList2()+"</div>");
$('.addressBook').append("<div class='test'> What is causing this in getContactList3:<br />"+
addressBook.getContactList3()+"</div>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="addressBook"></div>