Object property 属性有三种
- var 变量
- array 数组
- function 函数
var 变量
--- .property & ['property'] 2种方式
var beyond = {}; //define null object
beyond.formedIn = '1983'; // .property 的方式
beyond['foundedIn'] = '香港'; // ['property'] 的方式
array 数组
/**
* set proprty value when create an Object
*/
var beyond = {formedIn: '1983', foundedIn: '香港'};
以属性的方式赋值更加灵活
/**
* type of propery can be any kind of type including array
*/
var beyond = {
formedIn: '1983',
foundedIn: '香港',
artist: ['黄家驹', '黄家强', '黄贯中', '叶世荣']
};
以遍历的方式访问数组
/**
* travel array by 'for in'
*/
var property;
for (property in beyond) {
console.log(beyond[property]);
}
function 函数
/**
* add method called showArtist for beyond
*/
beyond.showArtist = function() {
for (var i = 0; i < this.artist.length; i++) { //this means object
document.writeln(this,artist[i]);
}
};
beyond.showArtist();