2013-02-27 90 views

回答

1

你可能只是先申報對象,然後將其推:

var people = []; 

var obj = { 
    name: "John", 
    height_in_cm : 190 
}; 
obj.height_in_inches = obj.height_in_cm * .39; 

people.push(obj); 

根據你的情況,你也可以創建一個「人」對象/類:

var person = (function personModule() { 
    function Person(props) { 
    this.name = props.name; 
    this.height_in_cm = props.height_in_cm; 
    this.height_in_inches = this.height_in_cm * .39; 
    } 

    Person.prototype = {...}; // public methods if necessary 

    return function(props) { 
    return new Person(props); 
    } 
}()); 

var people = []; 
people.push(person({ name: 'John', height_in_cm: 190 })); 

console.log(people[0].height_in_inches); //=> 74.1 
+0

謝謝。人類是我需要的:) – Reza 2013-02-27 06:43:42