JavaScript function arguments and calls

Source: Internet
Author: User

Function call:

/* 1. Function call */
var temp = distance (0,1,2,3);

/* 2. Method call */
This. CName = "global";
var o = {
CName: "Class O",
M:function () {
var = this;
Console.log (this = = O); True
Console.log (this.         CName); O Class
f ();
function f () {
Console.log (this = = O);
False, this is bound to the global object
Console.log (this. CName);
Global
Console.log (self = = O);
True
Console.log (self. CName);
O Class
}
}

};
O.M ();

About this:

When it is a method on an object, this is the current object.

Second, function call mode when a function is not a property of an object, it is called as a function. This mode is bound to the global object. By assigning this to that within an object, you can make the function pattern downgrade to that to access the specified object.

Arguments and formal parameters for the function:

/* 1. Optional Parameters */
function Getpropertynames (o,a) {
A = a| | [];
For (Var property in O) {
A.push (property);
}
return A;
}

var a = Getpropertynames (o);
var b;
Getpropertynames (P,B); Two kinds of calls

/* 2. Variable length arguments */
function Max () {
var max = 0;
for (var i=0; i<arguments.length; i++) {
if (Arguments[i]>max) {
Max=arguments[i];
}
return Max;
}
}

About arguments
function Test_1 (a,b,c,d) {
Console.log (A,B,C,D); 1,2,3,4
for (var i=0; i<arguments.length; i++) {
Arguments[i] = 0;
}
Console.log (A,B,C,D); 0,0,0,0
}
Test_1 (1,2,3,4);
Change Arguments[i], the corresponding parameters will also change

Caller&callee:

Caller: Functions that are executing functions
Callee: the function being executed
function Test_2 () {
Console.log ("test_2");

function Test_3 () {
Console.log ("Test_3");
function Test_4 () {
Console.log ("Test_4");
Arguments.callee (); This will loop the call Test_4
Test_4.caller ();//callback to Test_4 caller Test_3
}
return Test_4 ();
}
return Test_3 ();
}
Test_2 ();

can also be used to do recursion
var plus = function (x) {
if (x<=1) return 1;
Return X*arguments.callee (x-1);
}
Console.log (plus (5));

Parameter type detection:

Isfinite (); Whether the finite number
Isarraylike (); Whether it is an array

Custom properties of the function:

Uniqueinterger.count = 0
Defines a Count property for the Uniqueinterger function
function Uniqueinterger () {
var a= "MyFunc";
return uniqueinterger.count++;
}
Console.log (Uniqueinterger ()); 0
Console.log (Uniqueinterger ()); 1
Console.log (Uniqueinterger ()); 2

Functions as namespaces:

function MyModule () {
Module code
The variables used in this module are local variables
Does not pollute the global namespace
}
MyModule ()//Don't forget to call this function

or more simply.
(function () {
Module code
}());

JavaScript function arguments and calls

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.