2016-04-05 80 views
0

我有一個相當簡單的JavaScript對象與一些對象方法,我試圖從內部訪問/修改數組中的信息(存儲在全局變量中)這些方法之一。這裏是我的代碼:從Javascript中使用對象方法訪問全局變量

var list= []; 
function person(firstname, age){ 
    this.firstname = firstname; 
    this.age = age; 

this.addPerson = function(){ 
list.push({firstname, age }); 
} 

this.addPerson(); 

this.changeName = function (name, newname) { 
    var i = 0 
    for (i; i < list.length; i++);{ 
     if (list[i].firstname === name){ 
      list[i].firstname = newname; 
     } 
     } 
    } 
} 

var person = new Bunny('Jim', 20); 
var person = new Bunny('Sally',40); 
person.changeName('Jim', 'John'); 

當一個新人被創建時,他們會自動添加到我的列表中。當我調用changeName時,我想輸入我想要更改的名稱以及它應該是的新名稱。但是,我收到錯誤消息「Can not read property'firstname'of undefined,」這讓我認爲changeName無法訪問我的列表。

+0

函數'addPerson'的要點是什麼?你爲什麼不直接在構造函數中「推」人? – Lewis

+0

我寫了addPerson,因爲我想在創建一個新對象時立即將其推送到列表中。我無法調用addPerson();在構造函數內....我相信有更好的方法來做到這一點。你會怎麼做? – bjorkland

+0

只看到我的答案更好的方法。 – Lewis

回答

1

你有不同的問題,

  1. 的問題是與;後的使用for循環的括號。這實際上會導致循環體使用for循環的最後一個增量值運行一次。

    for (i; i < list.length; i++);{ 
    //---------------------------^ Remove it 
    

    這就是爲什麼你越來越不能使用Bunny讀取未定義錯誤

    for(var i=0;i<list.length; i++); //this will run repeatedly as per for loop semantics 
    
    { } //and this body will be executed only once with the final updated value of i 
    
  2. 您錯誤地啓動對象的屬性。在我們的例子中應該是 是person

    var person1 = new person('Sally',40); 
    person1.changeName('Jim', 'John'); 
    
+1

好抓:) :) –

+0

@FelixKling對不起,我感到困惑。找到真正的原因並更新它。 –

+0

啊!這工作!另外,「兔子」是其他東西遺留下來的一個愚蠢的神器......應該抓住了...... facepalm。謝謝!! – bjorkland

0

試試這個:

var list = []; 

var Bunny = function (firstname, age){ 
    //this.firstname = firstname; 
    //this.age = age; 

this.addPerson = function(){ 
list.push({'firstname':firstname,'age': age }); 
} 

this.addPerson(); 

this.changeName = function (name, newname) { 
    var i = 0 


    for (i; i < list.length; i++){ 

     if (list[i].firstname === name){ 
      list[i].firstname = newname; 
     } 
     } 
    } 
} 

var person = new Bunny('Jim', 20); 
var person = new Bunny('Sally',40); 
person.changeName('Jim', 'John'); 
console.log(list[0]); 

你有 ';'在for循環中,這是問題所在。

0

除了@ rajaprabhu的評論:

你有一個構造方法,人()。您應該將其重命名爲Person(),以符合構造函數方法的通用編碼標準。

您正在創建Person的2個實例(在使用Bunny的代碼中不正確)在「Person」對象的上下文中,您可以訪問其自己的作用域,這意味着您可以直接更改其自己的屬性。在循環查看changeName方法中的列表數組的位置,您正在達到對象範圍之外。用下面的1行代碼替換循環:

this.firstName = newName; 

此外,你不是推任何人進入你的列表數組。取而代之的

var person = new Bunny('Jim', 20); 

這樣做:

list.push(new Person('Jim', 20); 

這將一個新的Person對象添加到列表陣列。

全部編輯:

var list = []; 

function Person(firstname, age) { 
    this.firstname = firstname; 
    this.age = age; 

    this.addPerson = function() { 
    list.push({ 
     firstname, 
     age 
    }); 
    } 

    this.addPerson(); 

    this.changeName = function(name, newname) { 
    var i = 0 
    if (this.name == newname) { 
     this.name = newName; 
    } 
    } 
} 

// add the Person objects directly to the list array 
list.push(new Person('Jim', 20)); 
list.push(new Person('Sally', 40)); 

// now you can select an object in the list to change its name 
list[1].changeName('Jim', 'John'); 
+0

謝謝!這真的很有幫助! – bjorkland

1

除了一些語法錯誤,你的代碼仍然是設計不當。這是一個更好的。

function People(){ 
    this.list = []; 
} 
People.prototype.add = function(person){ 
    this.list.push(person); 
}; 
People.prototype.changeName = function(name,newName){ 
    for(var i=0;i<this.list.length;i++){ 
     var person = this.list[i]; 
     if(person.firstname === name){ 
      person.firstname = newName; 
     } 
    } 
}; 
function Person(firstname,age){ 
    this.firstname = firstname; 
    this.age = age; 
} 

//Usage 
var jim = new Person('Jim', 20); 
var sally = new Person('Sally',40); 

var people = new People(); 
people.add(jim); 
people.add(sally); 
people.changeName('Jim', 'John'); 
+0

啊,這真的很有幫助。我一直在努力嘗試一些原型。因此,People.prototype.add/People.prototype.changeName只是爲人定義了「add」和「changeName」方法,併爲人物對象製作了這些內置方法?希望很明顯......我對此還是有點新的。 – bjorkland

+0

@Lisa不,它不會那樣工作。這兩種方法不會分配給每個人。我在'People'類中添加'add'和'changeName'只是因爲它更合理。 – Lewis

+0

好的,謝謝 - 但是你仍然需要單獨調用add,而不是自動添加一個新創建的Person到列表中,這正是我最初想做的事情。但我想這只是更好的形式? – bjorkland