JS内置函数应用.docx
- 文档编号:6186871
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:21
- 大小:18.59KB
JS内置函数应用.docx
《JS内置函数应用.docx》由会员分享,可在线阅读,更多相关《JS内置函数应用.docx(21页珍藏版)》请在冰豆网上搜索。
JS内置函数应用
1extend
DOCTYPEhtml>
String.prototype.reverse=function(){
returnArray.prototype.reverse.apply(this.split('')).join('');
}
console.log('maizi'.reverse());
console.log('king'.reverse());
//检测方法是否存在,不存在则扩展
if(!
Array.prototype.inArray){
Array.prototype.inArray=function(needle){
for(vari=0,len=this.length;i if(this[i]==needle){ returntrue; } } returnfalse; } } vararr=['a','b','c','d']; console.log(arr.inArray('b')); console.log(arr.inArray("C")); functionPerson(){ this.mouth=true; } varp1=newPerson(); varp2=newPerson(); Person.prototype.say=function(){ return'hello'; } console.log(p1.say()); console.log(p2.say()); console.log(p1.constructor); console.log(p1.constructor.prototype.constructor); console.log(p1.constructor.prototype.mouth); Person.prototype={ hair: true, face: true }; console.log(typeofp1.hair); console.log(p1.say()); console.log(typeofp1.__proto__.say); console.log(typeofp1.__proto__.hair); varp3=newPerson(); console.log(typeofp3.__proto__.say); console.log(typeofp3.__proto__.hair); console.log(typeofp3.__proto__.face); console.log(p3.constructor); console.log(p1.constructor); console.log(typeofp3.constructor.prototype.hair); console.log(typeofp1.constructor.prototype.hair); Person.prototype={ 'hair': true, face: true }; Person.prototype.constructor=Person; varp4=newPerson(); console.log(p4.constructor); console.log(typeofp4.constructor.prototype.hair); 2重置constructor DOCTYPEhtml>
functionShape(){
this.name='shape';
this.toString=function(){
returnthis.name;
}
}
functionTwoDShape(){
this.name='2Dshape';
}
functionTriangle(side,height){
this.name='triangle';
this.side=side;
this.height=height;
this.getArea=function(){
returnthis.side*this.height/2;
}
}
TwoDShape.prototype=newShape();
Triangle.prototype=newTwoDShape();
//重置constructor属性
TwoDShape.prototype.constructor=TwoDShape;
Triangle.prototype.constructor=Triangle;
varmyTriangle=newTriangle(5,10);
console.log(myTriangle.getArea());
console.log(myTriangle.toString());
console.log(myTriangle.constructor);
console.log(myTriangleinstanceofTriangle);
console.log(myTriangleinstanceofTwoDShape);
console.log(myTriangleinstanceofShape);
console.log(Shape.prototype.isPrototypeOf(myTriangle));
console.log(TwoDShape.prototype.isPrototypeOf(myTriangle));
console.log(Triangle.prototype.isPrototypeOf(myTriangle));
console.log(Object.prototype.isPrototypeOf(myTriangle));
console.log(String.prototype.isPrototypeOf(myTriangle));
vartd=newTwoDShape();
console.log(td.constructor);
console.log(td.toString());
vars=newShape();
console.log(s.constructor);
console.log(s.toString());
3Shape
DOCTYPEhtml>
//functionShape(){
//this.name='shape';
//}
functionShape(){};
Shape.prototype.name='shape';
Shape.prototype.toString=function(){
returnthis.name;
}
functionTwoDShape(){};
TwoDShape.prototype=newShape();
TwoDShape.prototype.constructor=TwoDShape;
TwoDShape.prototype.name='2DShape';
functionTriangle(side,height){
this.side=side;
this.height=height;
}
Triangle.prototype=newTwoDShape();
Triangle.prototype.constructor=Triangle;
Triangle.prototype.name='triangle';
Triangle.prototype.getArea=function(){
returnthis.side*this.height/2;
}
varmyTriangle=newTriangle(5,10);
console.log(myTriangle.getArea());
console.log(myTriangle.toString());
console.log(myTriangle.hasOwnProperty('side'));
console.log(myTriangle.hasOwnProperty('name'));
console.log(myTriangleinstanceofTriangle);
console.log(myTriangleinstanceofTwoDShape);
console.log(myTriangleinstanceofShape);
console.log(Triangle.prototype.isPrototypeOf(myTriangle));
console.log(TwoDShape.prototype.isPrototypeOf(myTriangle));
console.log(Shape.prototype.isPrototypeOf(myTriangle));
console.log(Object.prototype.isPrototypeOf(myTriangle));
4字符串
DOCTYPEhtml>
functionShape(){};
Shape.prototype.name='shape';
Shape.prototype.toString=function(){
returnthis.name;
}
functionTwoDShape(){};
TwoDShape.prototype=Shape.prototype;
TwoDShape.prototype.constructor=TwoDShape;
TwoDShape.prototype.name='2DShape';
functionTriangle(side,height){
this.side=side;
this.height=height;
}
Triangle.prototype=TwoDShape.prototype;
Triangle.prototype.constructor=Triangle;
Triangle.prototype.name='triangle';
Triangle.prototype.getArea=function(){
returnthis.side*this.height/2;
}
varmy=newTriangle(5,10);
console.log(my.getArea());
console.log(my.toString());
vars=newShape();
console.log(s.name);
5属性构造
DOCTYPEhtml>
functionShape(){};
Shape.prototype.name='Shape';
Shape.prototype.toString=function(){
returnthis.name;
}
functionTwoDShape(){};
//声明临时构造器
varF=function(){};
F.prototype=Shape.prototype;
TwoDShape.prototype=newF();
TwoDShape.prototype.constructor=TwoDShape;
TwoDShape.prototype.name='2DShape';
functionTriangle(side,height){
this.side=side;
this.height=height;
}
varF=function(){};
F.prototype=TwoDShape.prototype;
Triangle.prototype=newF();
Triangle.prototype.constructor=Triangle;
Triangle.prototype.name='triangle';
Triangle.prototype.getArea=function(){
returnthis.side*this.height/2;
}
varmy=newTriangle(5,10);
console.log(my.getArea());
console.log(my.toString());
console.log(my.__proto__.__proto__.__proto__.constructor);
vars=newShape();
console.log(s.name);
6uber
DOCTYPEhtml>
functionShape(){};
Shape.prototype.name='Shape';
Shape.prototype.toString=function(){
varresult=[];
if(this.constructor.uber){
result[result.length]=this.constructor.uber.toString();
}
result[result.length]=this.name;
returnresult.join(',');
};
functionTwoDShape(){};
varF=function(){};
F.prototype=Shape.prototype;
TwoDShape.prototype=newF();
TwoDShape.prototype.constructor=TwoDShape;
TwoDShape.uber=Shape.prototype;
TwoDShape.prototype.name='2DShape';
functionTriangle(side,height){
this.side=side;
this.height=height;
}
varF=function(){};
F.prototype=TwoDShape.prototype;
Triangle.prototype=newF();
Triangle.prototype.constructor=Triangle;
Triangle.uber=TwoDShape.prototype;
Triangle.prototype.name='triangle';
Triangle.prototype.getArea=function(){
returnthis.side*this.height/2;
}
varmy=newTriangle(5,10);
console.log(my.getArea());
console.log(my.toString());
vartd=newTwoDShape();
console.log(td.toString());
7属性继承
DOCTYPEhtml>
functionextend(Child,Parent){
varF=function(){};
F.prototype=Parent.prototype;
Child.prototype=newF();
Child.prototype.constructor=Child;
Child.uber=Parent.prototype;
}
functionShape(){};
Shape.prototype.name='Shape';
Shape.prototype.toString=function(){
varresult=[];
if(this.constructor.uber){
result[result.length]=this.constructor.uber.toString();
}
result[result.length]=this.name;
returnresult.join(',');
};
functionTwoDShape(){};
extend(TwoDShape,Shape);
TwoDShape.prototype.name='2DShape';
functionTriangle(side,height){
this.side=side;
this.height=height;
}
extend(Triangle,TwoDShape);
Triangle.prototype.name='triangle';
Triangle.prototype.getArea=function(){
returnthis.side*this.height/2;
}
varmy=newTriangle(5,10);
console.log(my.getArea());
console.log(my.toString());
vartd=newTwoDShape();
console.log(td.toString());
8元素继承
DOCTYPEhtml>
functionextend(Child,Parent){
varF=function(){};
F.prototype=Parent.prototype;
Child.prototype=newF();
Child.prototype.constructor=Child;
Child.uber=Parent.prototype;
}
functionextend1(Child,Parent){
varp=Parent.prototype;
varc=Child.prototype;
for(variinp){
c[i]=p[i];
}
c.uber=p;
}
varShape=function(){};
varTwoDShape=function(){};
Shape.prototype.name='shape';
Shape.prototype.toString=function(){
returnthis.name;
}
extend(TwoDShape,Shape);
vartd=ne
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JS 内置 函数 应用