API mode in Javascript Mode

Source: Internet
Author: User
Javascript API mode: it helps us provide better and cleaner interfaces for functions. These modes mainly include the following: 1) callback mode: The function is also an object, so it can be passed as a parameter. See the following example: function demo01 (callback) {callback ();} function test01 () {console. log ("AAA") ;}demo01 (test01); // "AAA" Let's take a look at the callback and its scope: var myobj = {name: "zhangsan", setname: function (person) {person. name = This. name ;}}; function getname (callback) {var OBJ ={}; if (typeof callback === "function") {callback (OBJ);} return obj. name;} console. log (getname (myobj. setname); // return NULL characters, because this references all Bureau object (window) and window. name are defined. How can this problem be solved? Use callback and pass the object to which the callback belongs. VaR myobj = {name: "zhangsan", setname: function (person) {person. name = This. name ;}}; function getname (callback, callobj) {var OBJ ={}; if (typeof callback = "string") {callback = callobj [callback];} if (typeof callback = "function") {callback. call (callobj, OBJ);} return obj. name;} console. log (getname ("setname", myobj); // zhangsanconsole. log (getname (myobj. setname, myobj); // zhangsan2) configure the object mode. Advantages: a) You do not need to remember many parameters and their order. B) Optional parameters can be safely ignored. c) easy to read and maintain. d) easy to add and delete parameters. Disadvantages: A. Remember parameter name B when using it. For code optimization and compression, failed to compress var configure = {name: "zhangsan", sex: "male", age: 23} setperson (configure); // PASS Parameters 3) return function mode: A function is an object and can be returned as a return value. See the following function: var parent = function () {var name = "zhangsan"; return function () {return name ;}} console. log (parent (); // The zhangsanparent () function returns a function and creates a closure. We can use this closure to store some private data, the data can only be accessed by the returned function, but the external code is not accessible. The following is an example of a counter: var getcount = function () {var I = 0; return function () {return + I ;}} var Geti = getcount (); geti (); // 1 Geti (); // 2 Geti (); // 34) curry mode: Let's first create an addition example: vaR add = function (x, y) {return X + Y;} Add (1, 2) // 3 now run the following simulation in a single step: var add = function (x, y) {return 1 + Y;} var add = function (x, y) {return 1 + 2;} Let's add the curry function to meet our requirements: vaR add = function (x, y) {If (typeof y = "undefined") {return function (y) {return X + Y;} re Turn X + Y;} console. log (add (1); // functionconsole. log (add (1, 2); // 3console. log (add (1) (2); // 3 based on the above instance, we encapsulate a curry function mycurryfunction mycurry (FN) {var slice = array. prototype. slice, oldargs = slice. call (arguments, 1); return function () {var newargs = slice. call (arguments), argS = oldargs. concat (newargs); Return fn. apply (null, argS) ;}} function multi (A, B, C, D) {return a * B * C * D;} console. log (mycurry (multi, 1, 2) (3, 4 )); // 24 based on the above example, when should we use curry? When we find that the same function is called and the majority of parameters passed are the same, we can consider using the curry function.

 

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.