JS内置函数应用Word文件下载.docx
《JS内置函数应用Word文件下载.docx》由会员分享,可在线阅读,更多相关《JS内置函数应用Word文件下载.docx(21页珍藏版)》请在冰豆网上搜索。
)).join('
);
}
console.log('
maizi'
.reverse());
king'
//检测方法是否存在,不存在则扩展
if(!
Array.prototype.inArray){
Array.prototype.inArray=function(needle){
for(vari=0,len=this.length;
i<
len;
i++){
if(this[i]==needle){
returntrue;
}
}
returnfalse;
}
vararr=['
a'
'
b'
c'
d'
];
console.log(arr.inArray('
));
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(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(typeofp3.constructor.prototype.hair);
console.log(typeofp1.constructor.prototype.hair);
'
hair'
:
Person.prototype.constructor=Person;
varp4=newPerson();
console.log(p4.constructor);
console.log(typeofp4.constructor.prototype.hair);
2重置constructor
functionShape(){
this.name='
shape'
this.toString=function(){
returnthis.name;
}
functionTwoDShape(){
2Dshape'
functionTriangle(side,height){
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());
/script>
/body>
/html>
3Shape
//functionShape(){
//this.name='
//}
functionShape(){};
Shape.prototype.name='
Shape.prototype.toString=function(){
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.prototype.getArea=function(){
varmyTriangle=newTriangle(5,10);
console.log(myTriangle.getArea());
console.log(myTriangle.toString());
console.log(myTriangle.hasOwnProperty('
side'
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字符串
TwoDShape.prototype=Shape.prototype;
Triangle.prototype=TwoDShape.prototype;
varmy=newTriangle(5,10);
console.log(my.getArea());
console.log(my.toString());
vars=newShape();
console.log(s.name);
5属性构造
Shape'
//声明临时构造器
varF=function(){};
F.prototype=Shape.prototype;
TwoDShape.prototype=newF();
F.prototype=TwoDShape.prototype;
Triangle.prototype=newF();
console.log(my.__proto__.__proto__.__proto__.constructor);
6uber
varresult=[];
if(this.constructor.uber){
result[result.length]=this.constructor.uber.toString();
result[result.length]=this.name;
returnresult.join('
'
TwoDShape.uber=Shape.prototype;
Triangle.uber=TwoDShape.prototype;
vartd=newTwoDShape();
console.log(td.toString());
7属性继承
functionextend(Child,Parent){
varF=function(){};
F.prototype=Parent.prototype;
Child.prototype=newF();
Child.prototype.constructor=Child;
Child.uber=Parent.prototype;
extend(TwoDShape,Shape);
extend(Triangle,TwoDShape);
8元素继承
functionextend1(Child,Parent){
varp=Parent.prototype;
varc=Child.prototype;
for(variinp){
c[i]=p[i];
c.uber=p;
varShape=function(){};
varTwoDShape=function(){};
vartd=ne