// Add a method for the constructor prototype
Function. method = function (name, func ){
This. Prototype. Name = func;
}
Number. Method ("integer", function (){
Return math [This <0? 'Ceil ': 'floor'] (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
// Typical example of closure
For (VAR I = 0; I <Lis. length; 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 ));
}
})
// Recursive memory
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