One. Check whether it is a function.
function Isfunction (x) {
return Object.prototype.toString.call (x) = = = "[Object Function]";
};
function square (n) {
return n*n;
};
Alert (isfunction (square)); true
I. Functional programming
1. Working with arrays using functions
For example:
function fn () {//some code ...}; Elemnets.reduce (FN);
2. Higher-order functions--functions of operation functions
For example:
Function not (f) {
return function () {
var result = f.apply (this,arguments);
Return!result
}
};
var even = function (x) {return x%2 = = = 0};
var odd = not (even);
Alert ([1,1,3,5,7,9].every (odd)); //true to determine if it is an odd number
3. Incomplete functions
4. Memory
For example:
A function with memory capability
function memorize (f) {
var cache = {};
return function () {
var key = Arguments.length + Array.prototype.join.call (arguments, ",");
if (key in cache) return Cache[key];
else return Cache[key] = f.apply (this,arguments);
};
};
Greatest common divisor algorithm functions
function Zdgys (A, b) {
var T; if (a < b) {
t = B,b = A, a = t;
};
while (b! = 0) {
t = B,b = a%b, a = t;
};
return A;
};
var instance = memorize (Zdgys);
Alert (instance (27,54)); -
5.
201506250923_ JavaScript authoritative Guide (sixth edition)-checking for functions, incomplete functions, memory functions (P193-200)