The arguments object in Javascript hides xuanjicang... I used to be stupid?

Source: Internet
Author: User

I. Briefly introduce JavaScript arguments

The arguments object in Javascript is the object created during function execution. Through this function, we can dynamically obtain parameters. If you have learned Java, you may know that the method parameter type can be: type... parameter name, for example:

Class test {public static void dynamicparameter (string... arguments) {for (INT I = 0; arguments! = NULL & I <arguments. length; I ++) {system. out. println (arguments [I]) ;}} public static void main (string [] ARGs) {dynamicparameter ("A", "B", "C "); // print a B cdynamicparameter ("A", "B", "C", "D", "E") in sequence; // print a B c d e} in sequence }}

In this way, you can pass any string type object.

In JavaScript, the arguments object can be used to implement Java...

<SCRIPT type = "text/JavaScript"> function dynamicparameter (arg1) {alert (dynamicparameter. length); // This indicates that the function prototype declares several parameters: function dynamicparameter (arg1) Alert (dynamicparameter. arguments. length); // This indicates the number of parameters actually passed in. // dynamicparameter: // alert (arguments. length); // This indicates the actual parameters passed in for (VAR I = 0; I <arguments. length; I ++) {// alert (arguments [I]) ;}} dynamicparameter ("A", "B", "C "); // pop up a B cdynamicparameter ("A", "B", "C", "D", "E") in sequence "); // pop up a B c d e in sequence </SCRIPT>

2. Hidden xuanjicang ????

The story goes like this: Previously I encapsulated a series of general functions for the company. One of them is checkall, which is used to achieve full selection. This function is relatively simple. As defined at the beginning:

<! Doctype HTML> 

That is, when you click the Select All button, assign the checked value of the selected check box to the check box specified by other names. Then, when you look at the function today, it is not very concise, use event. srcelement, and then only one name parameter can be passed to achieve full selection, in order to be compatible with the previous code. The following code is written:

<SCRIPT type = "text/JavaScript">/*** checkall */function checkall (OBJ, checkboxname) {If (arguments. length = 1 & typeof (arguments [0]) = "string") {// if only one parameter is passed and the parameter type is stringobj = event. srcelement; // event. srcelement gets the object that triggers the event. Checkboxname = arguments [0]; // use the passed string as the checkboxname} var checkboxes = document. getelementsbyname (checkboxname); For (VAR I = 0; I <checkboxes. length; I ++) checkboxes [I]. checked = obj. checked ;}</SCRIPT> 

Everything seems so beautiful... but the tragedy has happened ....... No effect, so I tested it step by step.

Test 1:

Function checkall (OBJ, checkboxname) {alert (arguments [0]); // when I call checkall ('loves, here the lovesif (arguments. length = 1 & typeof (arguments [0]) = "string") {OBJ = event. srcelement; checkboxname = arguments [0];} var checkboxes = document. getelementsbyname (checkboxname); For (VAR I = 0; I <checkboxes. length; I ++) checkboxes [I]. checked = obj. checked ;}

Test 2:

Function checkall (OBJ, checkboxname) {If (arguments. length = 1 & typeof (arguments [0]) = "string") {alert (arguments [0]); // checkall ('loves '); here, loves OBJ = event is also displayed. srcelement; <span> </span> checkboxname = arguments [0];} var checkboxes = document. getelementsbyname (checkboxname); For (VAR I = 0; I <checkboxes. length; I ++) checkboxes [I]. checked = obj. checked ;}

Puzzling ....

Test 3:

Function checkall (OBJ, checkboxname) {If (arguments. length = 1 & typeof (arguments [0]) = "string") {OBJ = event. srcelement; alert (typeof (arguments [0]); // then I wrote a code like this and the result .. Guess what happened ?? Objectcheckboxname = arguments [0];} var checkboxes = document. getelementsbyname (checkboxname); For (VAR I = 0; I <checkboxes. length; I ++) checkboxes [I]. checked = obj. checked ;}

I don't believe my eyes... Therefore

Test 4:

function checkAll(obj,checkboxname){alert(arguments.length==1 && typeof(arguments[0])=="string");//trueif(arguments.length==1 && typeof(arguments[0])=="string"){alert(arguments.length==1 && typeof(arguments[0])=="string");//trueobj = event.srcElement;alert(arguments.length==1 && typeof(arguments[0])=="string");//falsecheckboxname = arguments[0];}var checkboxes = document.getElementsByName(checkboxname);for(var i=0;i<checkboxes.length;i++)checkboxes[i].checked = obj.checked;}

This time... I suddenly wanted to slap myself... I was not smart, but confused...

For this function, the objects referenced by OBJ and arguments [0] are actually one, but they are obtained in different ways.

When checkall ('loves '), both OBJ and arguments [0] are loves and then pass OBJ = event. srcelement; after OBJ and arguments [0], it actually points to the fully selected checkbox DOM object.

I take it for granted a bit... sorry

Why two failed publications 0 0


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.