Research on javascript arguments objects -- Research on jquery source code

Source: Internet
Author: User

Research on javascript arguments objects -- Research on jquery source code

External plug-ins:

$. Fn. tinytip = function (text, customOptions ){
Debugger;
If (text & typeof text === 'object '){
CustomOptions = text;
Text = customOptions. tooltip;
}

Var options = $. extend ({}, tooltip_options, customOptions );
Options. tooltip = text;
If (typeof options. tooltip === 'object '){
Options. content = options. tooltip;
Options. content. hide ();
}

......

....

 

Started with jquery source code

JQuery. extend = jQuery. fn. extend = function (){
Var options, name, src, copy, copyIsArray, clone,
Target =Arguments[0] | | {},
I = 1,
Length = arguments. length,
Deep = false;


// Handle a deep copy situation
If (typeof target = "boolean "){
Deep = target;
Target = arguments [1] || {};
// Skip the boolean and the target
I = 2;
}


// Handle case when target is a string or something (possible in deep copy)
If (typeof target! = "Object "&&! JQuery. isFunction (target )){
Target = {};
}


// Extend jQuery itself if only one argument is passed
If (length = I ){
Target = this;
-- I;
}

.....

 

1. In JavaScript, the arguments object is a special object, which is actually a built-in attribute of the current function. Arguments is very similar to Array, but it is not actually an Array instance. The following code can be used to prove the situation (in fact, in the funcArg function, calling arguments does not need to be written as funcArg. arguments, but can be directly written as arguments ).

Array. prototype. testArg = "test ";
Function funcArg (){
Console. log (funcArg. arguments. testArg );
Console. log (funcArg. arguments [0]);
Console. log (arguments [0]);
} S
Console. log (new Array (). testArg); // result: "test"
FuncArg (10); // result: "undefined" "10"

 

 

2. The length of an arguments object is determined by the number of arguments rather than the number of arguments. Parameters are the variables used to re-store the memory space in the function, but they do not overlap with the memory space of the arguments object. When both arguments and Values exist, the values are synchronized. However, if one of them has no values, the values without values cannot be synchronized. The following code can be verified.

Function f (a, B, c ){
Console. log (arguments. length); // result: "2"
A = 100;
Console. log (arguments [0]); // results: "100"
Arguments [0] = "qqyumidi ";
Console. log (a); // result: "qqyumidi"
Console. log (c); // result: "undefined"
C = 2012;
Console. log (arguments [2]); // result: "undefined"
}
F (1, 2 );

 

 

3,From the description and calling features of functions in JavaScript, we can see that functions in JavaScript cannot be overloaded.. Based on the reloads in other languages: "function return values are different or the number of parameters is different", we can conclude that:

First, the Javascript function declaration does not return value type;

Second, the number of parameters in JavaScript is strictly to facilitate variable operations in the function. In fact, the real parameters are already stored in the arguments object.

In addition, the JavaScript function itself has a deep understanding of why functions in JavaScript cannot be overloaded: In JavaScript,A function is actually an object. A function name is a reference to a function, or a function name is a variable.. The function declaration and function expression shown below are the same (without considering the differences between function declaration and function expression ), it is very helpful to understand that functions in JavaScript cannot be overloaded.

 

Function f (){
Return a + 10;
}


Function f (){
Return a-10;
}


// Without considering the difference between a function declaration and a function expression, it is equivalent to the following:


Var f = function (){
Return a + 10;
}


Var f = function (){
Return a-10;
}

 

 

4. The arguments object has a very useful property: callee.Arguments. callee returns the current function reference of the arguments object. We recommend that you replace the function name with arguments. callee when using a recursive function call.

Function count (){
Debugger;
If (a = 1 ){
Return 1;
}
Return a + arguments. callee (-- );
}


Var mm = count (10 );
Console. log (mm); // 55

 

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.