//方法1
//利用[]建立物件
var person = new Object();
person["firstname"] = "Yang";
person["lastname"] = "Danny";
//呼叫物件
console.log(person["firstname"]); //Yang
//可以放變數,去呼叫
var q = "lastname";
console.log(person[q]); //Danny
//方法2
//利用.建立物件
person.address = new Object();
person.address.country = "Taiwan";
person.address.city = "Taipei";
person.address.locate = "101";
//呼叫物件
console.log(person.address); //{country: "Taiwan", city: "Taipei", locate: "101"}
console.log(person.address.country); // Taiwan
console.log(person["address"]["locate"]);// 101
//方法3
//利用{}建立物件
var person = {
firstname: "Yang",
lastname: "Danny",
address:{
country: "Taiwan",
city: "Taipei",
locate: "101"
}
};
function sayHello(){
console.log("Hi "+person.lastname);
}
sayHello(); //Hi Danny