Research on JavaScript arguments object--Research on jquery source code

Source: Internet
Author: User

External plugins:

$.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 ();
}

。。。。。。

。。。。


From the source of jquery

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 if 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 more specific object, which is actually a built-in property of the current function. Arguments is very similar to an array, but is not actually an array instance. Can be confirmed by the following code (of course, in the function Funcarg, the call arguments is not necessarily written funcarg.arguments, write arguments directly).

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 the arguments object is determined by the number of arguments and not by the number of parameters. A parameter is a variable inside a function that re-opens the memory space, but it does not overlap with the arguments object memory space. In the case of arguments and values, the values are synchronous, but for one of the none, the value of this no value is not synchronized. The following code can be verified.

function f (A, B, c) {
Console.log (arguments.length); Result: "2"
A = 100;
Console.log (Arguments[0]); Result: "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, the function in JavaScript, the Declaration and invocation of features, you can see that the JavaScript function is not overloaded . Depending on the overloads in other languages: "The return value of the function is different or the number of formal parameters is different", we can draw the above conclusion:

First: The declaration of a JavaScript function is not a return value type;

Second: The number of formal parameters in JavaScript is strictly in order to facilitate the manipulation of variables in the function, and the actual arguments are already stored in the arguments object.

In addition, the JavaScript function itself is a deep understanding of why a function in JavaScript cannot be overloaded: In JavaScript, a function is actually an object, a function name is a reference to a function, or the function name itself is a variable . For function declarations and function expressions as shown below, it is very useful to understand that a function in JavaScript cannot be overloaded without considering the difference between a function declaration and a function expression.


function f (a) {
return a + 10;
}


function f (a) {
return a-10;
}


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


var f = function (a) {
return a + 10;
}


var f = function (a) {
return a-10;
}



4. There is a very useful attribute in the arguments object: callee. Arguments.callee Returns the current function reference where this arguments object resides . It is recommended to use Arguments.callee instead of the function name itself when using recursive function calls.

function count (a) {
Debugger
if (a==1) {
return 1;
}
Return a + Arguments.callee (--a);
}


var mm = count (10);
Console.log (mm); 55


Research on JavaScript arguments object--Research on jquery source code

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.