JavaScript fun: ing of form parameters and real parameters

Source: Internet
Author: User
As an extension of function functions, you need to establish a correct ing relationship between its form parameters and real parameters when a function is called. Let's take a look at the following example: as an extension of function functions, you need to establish a correct ing relationship between its form parameters and real parameters when a function is called.

Let's take a look at the following example:

function func1(arg1, arg2) { ... }    var map = createArgumentMap(func1,'valueOfArg1', 'valueOfArg2');  console.log(map['arg1']);  // writes 'valueOfArg1'  console.log(map['arg2']);  // writes 'valueOfArg2'

This ing function receives a function object as the first parameter, and a list of real parameters with unknown length. You must create a ing between the form parameters and the real parameter list of this function object, it is represented by an associated array and returned.

The real parameter list and function parameters are defined in the same order and are valid and valid parameters.

This topic is to create a ing between the form parameter and the real parameter. First, we obtain the toString () string form of the function object and extract its list of form parameters.

Then, because the length of the real parameter list is unknown, you can use the Array. prototype. slice Method to set its context to an arguments object, extract the real parameter from Index 1, and obtain the Array form of the real parameter list.

Finally, an associated array is created based on the real parameter list, the form parameter is key, the real parameter is value, and the associated array is returned.

This completes the parameter ing function.

String. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g, "") ;}; var findArgs = function (funcStr) {var bracket1 = funcStr. indexOf ("("); var bracket2 = funcStr. indexOf (")"); var argsStr = funcStr. slice (bracket1 + 1, bracket2); var args = argsStr. split (","); return args. map (function (e) {return e. trim () ;}) ;}; function createArgumentMap (func) {var args = findArgs (func. toString (); var realArgs = Array. prototype. slice. call (arguments, 1); var map ={}; for (var I = 0; I
 
  

The above is the JavaScript interesting question: the ing between the form parameter and the real parameter. For more information, see the PHP Chinese website (www.php1.cn )!

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.