JavaScript Ninja reading essay (i): function declaration, invocation

Source: Internet
Author: User

In JavaScript, functions exist as first-class members, so it is very necessary to master the knowledge of functions in JavaScript, and in recent days read the third and fourth chapters of the JavaScript Ninja and make a summary.

JavaScript function declaration:

JavaScript functions are declared using function literals to create functions.

Shaped like

Function Name (ARG1,ARG2)//The name of the function is optional {code;};

Scope of the function:

There are no block-level scopes in JavaScript, only function scopes. That is, in JavaScript, scopes are declared by function, not by code blocks.

{    var a=10;} Console.log (a);//The result is ten
function A () {    var a=10;} Console.log (a);//The result is undefined

Function call:

1. Calling as a function

function A () {};a ();

2. Calling as a method

var o={};o.haha=function(); haha ();

3. Call as a constructor

function Pig () {}; var xiaohong=New  Pig (); var xiaoming=New Pig ();

4. Using the Apply and call method calls

function haha () {}; var hahaha1={}; var hahaha2={};haha.apply (hahaha1,[1,2,3,4]); // The first argument of the apply method is the object of the function context, the second argument is the array haha.call (hahaha2,1,2,3,4) to pass in the parameter, the first argument of the call method is the object of the function context, and the remaining arguments are the arguments to pass in

JavaScript Ninja reading essay (i): function declaration, invocation

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.