201506250923_ JavaScript authoritative Guide (sixth edition)-checking for functions, incomplete functions, memory functions (P193-200)

Source: Internet
Author: User
Tags greatest common divisor

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.