Summary of JavaScript functions

Source: Internet
Author: User

JavaScript functions are a class-one citizen, functions can exist alone, regardless of function definition within that function, can be called by other objects

For example:

function person (name)

{

var walk = function ()

{

Alert ("I'll Run");

}

}


Walk.call (window);

Although the walk function is a person, the function can be called by another object.



Methods for defining functions


The first type:

functoion function Name (P1, p2)

{

function execution Body;

return p1 + p2;

}


The second type//use more

var tt = fuction (P1, p2)

{

return p1 + p2;

}


Third Kind

New function (P1,P2, P3......PN)

{

PN is the executing body;

}


Focus: Each time you define a JS function, you produce one class at a time.


----------------------------------------------

Use the difference without using Var:

→ If you use var to define a variable, the program will force a new variable to be defined.

→ If a variable is not defined using VAR, the system will first search for the variable in the current context, and the system will redefine a new variable only if the variable does not exist.


-----------------------------------------------

JavaScript is not overloaded with methods, it is according to the latest definition of the method


-----------------------------------------------


function call;

Call directly:

P.walk ();


Invoke the function with the call () method;

Function name. Call (caller, parameter 1, parameter 2 ...);


The caller. function (parameter 1, parameter 2, ...) = functions name. Call (caller, parameter 1, parameter 2 ...)


Call with the Apply () method

Name of the function. Apply (caller, arguments)

For example myfun.apply (window, [12, 3, 4]);

Summary of JavaScript functions

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.