1.直接给对象扩充属性和方法;
2.对象字面量;
3.工厂方式;
4.构造函数方式;
5.原型方式;
6.混合方式。
<script> // 1.直接给对象扩充属性和方法; var cat = {}; cat.name = '小白'; cat.color = 'blue'; cat.food = 'fish'; cat.skill = function () { alert('喵喵~~~'); } cat.skill(); //2.对象字面量 var cat2 = { name: '小白', color: 'blue', food: 'fish', skill: function () { alert('喵喵~~~'); } }; console.log(cat2.name); //3.工厂方式 function cat4(n, c) { var cat = {}; cat.name = n; cat.color = c; cat.food = 'fish'; cat.skill = function () { alert('喵喵~~~'); } return cat; } var cat5 = cat4('小白','red'); console.log(cat5.name); console.log(cat5.color); //4.构造函数方式 function Cat6(n,c) { this.name = n; this.color = c; this.food = 'fish'; this.skill = function () { alert('喵喵'); } } var cat7 = new Cat6('小黄','yellow'); console.log(cat7.name); console.log(cat7.color); //5.原型方式 function Cat8() {}; Cat8.prototype.name = '小白'; Cat8.prototype.color = 'white'; Cat8.prototype.food = 'fish'; Cat8.prototype.skill = function () { alert('喵喵~~~'); } var cat9 = new Cat8(); console.log(cat9.food); //6.构造函数混合方式 function Cat(n, c) { this.name = n; this.color = c; this.food = 'fish'; } Cat.prototype.skill = function () { alert('喵喵hahah~~~'); } var cat10 = new Cat('红','red'); console.log(cat10.name); cat10.skill(); </script>