大苹果
JavaScript之封装
封装es5封装functionPerson(name,sex,age){if(!(thisinstanceofPerson)){returnnewPerson(name,sex,age);}this.name=name;this.sex=sex||'female';this.age=age||0;this.walk=function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}this.study=function(skill){console.log('学习'+skill);}this.introduce=function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}//调用方式//new关键字创建实例varp=newPerson('小名','male',10);//直接调用创建varp1=Person('小红','female',9);p.walk();//走路p1.study('游泳');//学习游泳p.introduce();//我是小名,我是一个男孩,今年10岁了。p1.introduce();//我是小红,我是一个女孩,今年9岁了。原型链的方式functionPerson(name,sex,age){if(!(thisinstanceofPerson)){returnnewPerson(name,sex,age);}this.name=name;this.sex=sex;this.age=age;}Person.prototype.walk=function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}Person.prototype.study=function(skill){console.log('学习'+skill);}Person.prototype.introduce=function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}//调用方式//new关键字创建实例varp=newPerson('小名','male',10);//直接调用创建varp1=Person('小红','female',9);p.walk();//走路p1.study('游泳');//学习游泳p.introduce();p1.introduce();es6封装classPerson{constructor(name,sex,age){this.name=name;this.sex=sex;this.age=age;}walk(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}study(skill){console.log('学习'+skill);}introduce(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}//调用方式//new关键字创建实例varp=newPerson('小名','male',10);p.walk();//走路p.introduce();//直接调用创建//varp1=Person('小红','female',9);//TypeError:ClassconstructorPersoncannotbeinvokedwithout'new'//class定义的不能直接调用,只能通过new关键字实例化后调用console.log(typeofPerson);//function继承es5原型链实现继承functionPerson(name,sex,age){if(!(thisinstanceofPerson)){returnnewPerson(name,sex,age);}this.name=name;this.sex=sex||'female';this.age=age||0;this.walk=function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}this.study=function(skill){console.log('学习'+skill);}this.introduce=function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}functionBoy(name,age){this.name=name;this.age=age;this.sex='male';this.doHouseWork=function(){console.log('我在做家务');}}Boy.prototype=newPerson();Boy.prototype.constructor=Boy;varboy=newBoy('汤姆',12);boy.introduce();//我是汤姆,我是一个男孩,今年12岁了。boy.doHouseWork();//我在做家务console.log(boyinstanceofBoy);//trueObject.createfunctioncreate(obj){returnObject.create(obj);}varperson={name:'Tom',age:20,sex:'male',walk:function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');},study:function(skill){console.log('学习'+skill);},introduce:function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}};varboy=create(person);boy.age=15,boy.name='晓东';boy.sex='male';boy.doHouseWork=function(){console.log('我在做家务');}boy.introduce();//我是晓东,我是一个男孩,今年15岁了boy.doHouseWork();//我在做家务call方法functionPerson(name,sex,age){if(!(thisinstanceofPerson)){returnnewPerson(name,sex,age);}this.name=name;this.sex=sex||'female';this.age=age||0;this.walk=function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}this.study=function(skill){console.log('学习'+skill);}this.introduce=function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}functionBoy(name,age){varobj=Person.call(this,name,'male',age);obj.doHouseWork=function(){console.log('我在做家务');}returnobj}letboy=Boy('小米',16);boy.introduce(boy);//我是小米,我是一个男孩,今年16岁了。boy.doHouseWork();//我在做家务apply方法functionPerson(name,sex,age){if(!(thisinstanceofPerson)){returnnewPerson(name,sex,age);}this.name=name;this.sex=sex||'female';this.age=age||0;this.walk=function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}this.study=function(skill){console.log('学习'+skill);}this.introduce=function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}functionBoy(name,age){varobj=Person.apply(this,[name,'male',age]);obj.doHouseWork=function(){console.log('我在做家务');}returnobj}letboy=Boy('小米',17);boy.introduce(boy);//我是小米,我是一个男孩,今年16岁了。boy.doHouseWork();//我在做家务es6extends关键字classPerson{constructor(name,sex,age){this.name=name;this.sex=sex;this.age=age;}walk(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}study(skill){console.log('学习'+skill);}introduce(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}classBoyextendsPerson{constructor(name,age){super(name,'male',age);}doHouseWork(){console.log('我在做家务');}}varboy=newBoy('汤姆',14);boy.introduce();//我是汤姆,我是一个男孩,今年12岁了。boy.doHouseWork();//我在做家务console.log(boyinstanceofBoy);//true多态函数参数不定个数functionPerson(name,sex,age){if(!(thisinstanceofPerson)){returnnewPerson(name,sex,age);}this.name=name;this.sex=sex||'female';this.age=age||0;this.walk=function(){if(this.age<=2){returnconsole.log('我不会走路');}if(this.age>2&&this.age<4){returnconsole.log('我会走路了');}returnconsole.log('走路');}this.study=function(skill){console.log('学习'+skill);}this.introduce=function(){console.log(`我是${this.name},我是一个${this.sex==='male'?"男":"女"}孩,今年${this.age}岁了。`);}}functionMathematician(name,age){this.sex='male';this.calc=function(){varargsLength=arguments.length;switch(argsLength){case1:console.log("这是"+arguments[0]);break;case2:console.log(arguments[0]+'+'+arguments[1]+'='+(arguments[0]+arguments[1]));break;default:console.log('太复杂的暂时不会');}}}Mathematician.prototype=newPerson();Mathematician.prototype.constructor=Mathematician;varboy=newMathematician();boy.calc();//太复杂的暂时不会boy.calc(1);//这是1boy.calc(1,3);//1+3=4
JavaScript
185
7月前