Js obtains the implementation code of the current function parameter object.

Source: Internet
Author: User

Js obtains the implementation code of the current function parameter object.

Sometimes, when encapsulating controls, you need to obtain the input direct parameters or object parameters in many Js functions. Therefore, we need to judge the first object every time, therefore, to encapsulate a function here to obtain the parameter values of the current function:

/* -------------------------------------- * Clear spaces at both ends of the String, including line breaks and tabs * ---------------------------------------- */String. prototype. trim = function () {return this. replace (/(^ [\ s \ n \ t] + | [\ s \ n \ t] + $)/g ,"");} /* optional * Get the parameter object of the current function * -------------------------------------- * case-sensitive diffCase. The default value is false * ---------------------------------- */function GetArgs (diffCase) {// Return the parameter Object var result = new Object (); // obtain the call function var caller = arguments. callee. caller; if (caller = null | caller. arguments. length = 0) return result; // obtain the function's parameter set var matchs = caller. toString (). match (/\ s * function [\ w \ s] * \ ([\ w \ s,] *) \)/); if (matchs = null) return result; var argArray = matchs [1]. split (","); // get the parameter object var params = caller. arguments [0]; var index = typeof (params) = "object "? 1: 0; if (index = 1) {for (var p in params) {for (var I = 0; I <argArray. length; I ++) {var arg = argArray [I]. trim (); if (diffCase) {if (arg = p) {result [arg] = params [p]; break ;}} else {if (arg. toLocaleLowerCase () = p. toLocaleLowerCase () {result [arg] = params [p]; break ;}}}}} // multiple parameters overwrite the parameters after the first one to the for (var I = index; I <argArray. length & I <caller. arguments. length; I ++) result [argArray [I]. trim ()] = caller. arguments [I]; return result ;}

Call example:

// Test function Test (name, age) {// obtain the parameter object var args = GetArgs (); alert ("name:" + args. name + ", age:" + args. age) ;}// call the Test ("Zhang San", 25); Test ({name: "Li Si", age: 30}); Test ({name: "Wang Wu"}, 18 );

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.