1.原型对象的使用
function Person(name,age){ this.name=name; this.age=age; } Person.prototype.eat=function () { console.log("吃凉菜"); }; //使用原型来解决数据共享的问题 var p1=new Person("小明",20); var p2=new Person("小明",20); console.dir(p1); p2.eat(); //作用是解决数据共享的问题
2.面向对象的使用
unction ChangeStyle(btnObj,dvObj,json){ this.btnObj = btnObj; this.dvObj = dvObj; this.json = json; } ChangeStyle().prototype.init=function(){ //改变按钮,改变多个div多个样式的属性 var that=this; this.btnObj.onclick=function(){ for(var key in that.json){ that.dvObj.style[key]=that.json[key]; } } } //实例化对象 var json = {"width": "500px", "height": "800px", "backgroundColor": "blue", "opacity": "0.2"}; var cs = new ChangeStyle(my$("btn"), my$("dv"), json); cs.init();//调用方法