JSobject-oriented面向对象的主要内容:1.属性事务的特点2.方法事务的作用3.一个对象的实例对象原型:prototype——存储一个对象的内存地址。原型属于对象子对象函数构造函数,函数构造函数,创建函数对象varobj=newFunction(var1,var2,...,functionBody());//var1,var2正常通信functionBody()自定义函数//注意:构造函数构造的对象是低效的,var1,var2的顺序不能在functionBody中改变闭包:闭包是一个表达式(通常是一个函数),它有很多变量和绑定这些变量的环境。变量作用域有两种:全局作用域,局部作用域全局变量可以在函数内部访问在函数内部,所有用var声明的都是局部变量闭包:特点:函数b是嵌套在a中的函数,a需要返回functionb用途:1.读取函数内部变量2.让i变量的值保存在内存中闭包优缺点:优点:封装性好,可以访问局部变量缺点:1.内存占用时间长了,浪费内存,容易出现内存泄露。声明对象的方式:1.varperson={name:'zhangsan',age:26,eat:function(fds){alert("我在吃东西");},play:function(ga){alert("玩游戏");}}person.name//调用属性person.eat("noodles");2.new运算符后跟对象构造函数varobj=newObject();对象。属性=属性值;对象。属性=属性值;对象。method=function(str){方法代码};3.js中的构造方法声明对象functiontest([parameterlist]){this.property=propertyvalue;this.method=function(){方法代码}}varobj=newtest(参数列表);this代表当前对象,在4.js中具体化了一个工厂方法声明对象functioncreateObject(name,age){varobj=newObject();obj.name=名称;obj.age=年龄;obj.run=function(){returnthis.name+this.age+'Running...';};returnobj;}varbox1=createObject('zhangsan',100);varbox2=createObject('lisi',200);同一模式下创建的对象是相对独立的。Howtorun加载内存准备执行构造方法不会显示对象创建,给this赋属性,不需要返回对象工厂必须在方法内部创建对象对象,还需要返回对象对象。属性和方法都赋值给object对象。5.jsPrototype方式声明对象:functiontest(){}test.prototype.property=propertyvalue;test.prototype.property=属性值;测试.原型。方法名=函数(){执行代码}varobj=newtest();函数本身声明为空内容,使用prototype定义一些属性和方法,使得所有的实例对象都有它包含的属性,而方法test.prototypeinstanceofObject就是对functiontest(){}test.prototype.color="red";test.prototype.heights="1.7";test.showInfo=function(){alert(this.color+this.height);}test.getinfo=function(){altert("aaa");}varcar1=newtest();car1.getinfo();原型模式也可以:functiontest(){}test.prototype={color:“red”;…..}6.混合模式函数测试(v1,v2,v3){this.v1=v1;这个.v2=v2;this.v3=v3;}test.prototype.methodname=function(){执行代码}varobj=newBlog(v1,v2,v3);例如unctionblog(name,url,friend){this.name=name;这个.url=url;this.friend=friend;}blog.prototype={showinfo:function(){alert(this.name+this.url);}gets:function(){alert(this.friend);}}varpeo=newblog("","","");peo.showinfo();**对象遍历varren={};遍历对象的属性,可以将对象看成一个数组forinren.name="zhangsan";ren.age=18;ren.len="1.80";ren.demo=function(){alert(this.name);}for(variinren){alert(ren[i]);//获取属性的值或方法的代码}如果i是属性或方法的名称构造函数:functionren(){this.name="zhangsan";this.age="19";this.demo=function(){alert(this.name);}}varr=newren()for(variinr){alert(r[i]);}对象的存储对象放在栈内存中,指向堆内存中的地址。堆内存包含了所有的属性和方法,但是如果是方法的话,方法里面会有需要执行的代码,那些代码会放在代码段中,所有的对象都是相互独立封装的隐藏高内部数据和操作细节的吸收,只提供外部调用。在js中可以使用闭包来实现封装,所以最好选择函数作用域,例如1functiondemo(){varn=1;functiontest(){//特权方法return++n;}返回测试;}varat=demo();具有真正封装的意思e.g.2functionA(){function_xx(){alert(11);}this.xx=function(){return_xx;}}A.prototype={oth:function(){alert("普通方法");}}vara=newA();varb=a.xx();b();//print11原型和原型链原型:使用原型添加属性和方法原型链:js创建对象时,会内置一个属性proto,用来指向函数对象的原型对象创造了它。prototypejs继承:过程:varperson=function(){};varp=newperson();//三个阶段1.varp={}//创建对象2.p._proto_=person.prototype_proto_自带A属性3.创建对象(初始化对象)p————>person.call(p)e.g.//jsinheritancevarperson=function(){}person.prototype.say=function(){alert("天气很好");}varp=newperson();p.say();//phasnosaymethod//p._proto_=person.prototype(hasthisAttributes)-----有say方法person.prototype.gongzi=500;varprogrammer=function(){};programmer.prototype=newperson();programmer.prototype.scd=function(){alert("明天天气也不错");}programmer.prototype.gongzi=1000;varp=newprogrammer();p.say();//可以调用p.wcd();alert(p.gongzi);//1000;//原型链实现过程varp=newprogrammer();p._proto_=programmer.prototype=newperson();varp1=newperson();programmer.prototype=p1p.say();——>p._proto_——>programmer.prototype==p1——>p1._proto_==person.prototype.say();当两者属性相同时,比如gongzi,child会覆盖parent的关键字js面向对象关键字1.instanceof:变量是否是一个对象的实例vararr=newArray();alert(arrinstanceofArray);2.delete:删除对象属性变为undefined,不能删除方法?,不能删除变量,不能删除原型链中的属性和方法3.call,apply:带call的对象默认只能引用已有的对象add.call(subs,5,3);括号被替换,subs-->addadd.apply(sub,[5,3]);例如functionanimal(){this.name="ani";this.showName=function(){alert(this.name);}}functioncat(){this.name="cat";}varan=newanimal();varc=newcat();an.showName.call(c,",");//猫,通过showName方法通过call方法传给cat。如果在真例中使用example,将对象中的方法传递给后续的对象。4.arguments的使用:每个函数都有一个arguments对象的实例参数。引用函数的参数(实际参数)可以通过数组下标来引用。:返回正在执行的函数对象,函数内容arguments.callee默认值,正在执行的函数对象callee视为一个属性,函数内容不能加括号6.如何使用this1.此函数调用函数测试(){这个。x=1;//这个全局变量globalalert(this.x);}text();等于:varx=1;functiontest(){this.x=0;//改变全局变量的值}text();警报(x);//02。作为方法调用,构造函数中的this引用当前对象functiontest(){this.name="zhangsan";//this表示当前对象this.age=18;}vart=newtest();alert(t.name);3.调用并将this指向第一个参数varx=0;functiontest(){alert(this.x)}varo={};o.x=1;o.m=testo.m。apply();//0o.m.apply(o);//1对象伪装把父类的属性和方法一起传给子类,做一个对象伪装//对象伪装函数person(name,age){this.name=名称;这个。年龄=年龄;functionsayHi=function(){alert(“hi”);}}person.prototype.walk=function(){alert(“walk….”);}functionstudent(name,age,grade){this.newMethod=person;//伪装成person对象,传递特权属性和特权方法子类this.newMethod(name,age);this.grade=grade;}vars1=newstudent(“zhangsan”,15,5);//s1为学生对象,继承person,具有person的所有属性和方法alert(s1.name);s1.walk();//对象模拟错误,特权属性没有问题,但是继承的特权属性没有继承普通方法和属性构造函数的继承在子类内部构造父类的对象实现继承函数parents(name){this.name=name;this.say=函数ion(){alert("父亲的名字");}}functionchild(name,age){this.pObj=parents;这个.pObj(名字);这个。年龄=年龄;this.sayC=function(){alert("child"+this.name+"----"+"age:"+this.age);}}varp=newparents("zhangsan");p.say();//爸爸叫shangsanvarc=newchild("李四",20);c.sayC();//this.pObj(name);——parents(name)——-this.name=name="李四"父对象被对象继承,所有属性和方法都传递给子对象call和applycall的用法是调用一个对象的方法,并用另一个对象替换当前对象...)应用-obj.apply(方法,[var1,var2,....]);functionperson(name,age,len){this.name=name;这个。年龄=年龄;这个.len=len;this.say=function(){alert(this.name+”:”+this.age+”:”+this.len);}}functionstudent(name,age)[person.call(this,name,age);}varperson=newperson("张三",25,"170");每说();varstu=newstudent("李四",18);stu.say();//李四18undefined//person.call中的this变成stuapply继承:functionteacher(name,len){person.apply(this.[name,age,len]);}vartea=newteacher(“……”,”……”,”……”);tea.say();//…………