Home  >  Article  >  Web Front-end  >  javascript essence notes_javascript skills

javascript essence notes_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:51910browse

//Add a method to the constructor prototype
Function.method=function(name,func){
 this.prototype.name=func;
}

Number.method("integer" ,function(){
 return Math[this });
(-10/3).integer();//- 3

String.method("trim",function(){
return this.replace(/^s |s $/g,'');
})
" neat ".trim();//neat
//closure
var quo = function(status){
return{
get_status:function(){
return status;
}
 }
}
var myQuo = new quo("amazed");
myQuo.get_status();//amazed

//Closed classic example fragment
for(var i=0;i lis.onclick=function(i){
return function(){
alert(i);
};
 }(i);
}

//Apply
Function.method("curry",function(){
 var slice=Array.prototype.slice,
args = slice.apply(arguments),
that = this;
return function(){
return that.apply(null,args.contact(arguments));
}
})

//Memory recursion
var memoization = function(memo,usefn){//Abstraction
var fn = function(n){
var result = memo[n] ;
if(typeof result!=='number'){
result=usefn(fn,n);
memo[n]=result;
}
return result;
};
return fn;
};
var factorial= memoization([0,1],function(fn,n){//Recursive form
return fn(n-1) *n
});
factorial(5)//120

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn