利用字面量
// 利用字面量
var button = {
width: '150px',
height: '300px',
backgroundcolor: 'red',
action: function () {
console.log('点击我');
}
}
console.log(button.height);
button.action();
new Object
var button = new Object();
button.width = '900px';
button.height = '400px';
button.background = 'red';
button.action = function () {
console.log('利用关键字点击');
}
console.log(button.width);
button.action();
利用构造函数创建对象
function Button(width, height, background) {
this.width = width;
this.height = height;
this.background = background;
this.action = function (click) {
console.log(clik);
}
}
var abutton = new Button('300px', '900px');
console.log(abutton.height);
abutton.action('利用构造函数点击');