//定义JavaScript对象的几种格式
// Method 1: flat array style quick object define
var myObject = {
username : "beansoft",
age : 24,
test : function() {alert(this.age);}
};
// Method 2: using Object
var myObject = new Object();
myObject.username = "beansoft";
myObject.age = 24;
// Method 3: using constructor
function MyObject(username, age) {
this.username = username;
this.age = age;
this.test = function() {alert(this.age);};
}
var myObject = new MyObject("beansoft", 24);
// Using: myObject.username, myObject["username"], myObject[0]
myObject.test();// Will display alert window, value is age
myObject.username = "Hello";// Will asign the username property to "Hello"
回复 更多评论