iT邦幫忙

0

JS 建立物件的方法

js
  • 分享至 

  • xImage
  •  
//方法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

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言