網頁全端開發JavaScript_0926
新增物件
let 物件名稱 = {
屬性 (key:值)
行為 (methods)
};
存取物件的屬性有兩種方式
let Wilson = {
first_name : "Wilson",
last_name : "Ren",
age : 26,
is_married : true,
!!12345!! : "Hello Hello",
"002" : "yaya",
};
1.特性存取(property access).
console.log (Wilson .first_name);
console.log (Wilson .name);
// Wilson
// undefined
2.鍵值存取(key access)[ ]
console.log (Wilson ["age"] );
console.log (Wilson ["!!12345!!"] );
console.log (Wilson ["002"] );
// 26
// "Hello Hello"
// "yaya"
*若要使用一些包含特殊字符或動態產生的字串作為屬性名稱,就必須使用鍵值存取 [ ] 的方式
*若存取物件中不存在的屬性,此時會回傳 undefined
物件方法 (Object Method)
this 用法
let Wilson = {
first_name : "Wilson",
last_name : "Ren",
age : 26,
is_married : true,
'!!12345!!': "Hello Hello",
sayHi () {
console.log( this.first_name + "say hi" )
},
walk () {
console.log( this.first_name + "is walking..." )
}
};
*要存取物件本身的資料或屬性時,可以用關鍵字 this
電腦科學中,時間複雜度是指
在參數逐漸變大的情況下,function執行完畢所需的時間的成長速度