JavaScript defines a function method _javascript tips

Source: Internet
Author: User
Tags function definition pow

JavaScript uses keyword function to define functions.

A function can be declared, or it can be an expression.

function declaration
In previous tutorials, you've learned about the syntax of function declarations:

function functionname (parameters) {
executed code
}

The function declaration is not executed immediately, and will be invoked when we need it.
instance

function MyFunction (A, b) {return
a * b;
}

function expression

JavaScript functions can be defined by an expression.
A function expression can be stored in a variable:

Instance

var x = function (A, b) {return a * b};

When a function expression is stored in a variable, the variable can also be used as a function:
Instance

var x = function (A, b) {return a * b};
var z = x (4, 3);

The above function is actually an anonymous function (the function has no name).
Functions are stored in a variable and do not require a function name, usually called by the variable name.

function () constructor
in the above example, we learned that functions are defined by keyword function.
Functions can also be defined by the built-in JavaScript function constructor (function ()).

var myfunction = new Function ("A", "B", "return A * b");
var x = MyFunction (4, 3);

In fact, you don't have to use constructors. The above example can be written as:

var myfunction = function (A, b) {return a * b}
var x = MyFunction (4, 3);

function elevation (hoisting)
In previous tutorials we have already learned about the "hoisting (Ascension)".
Elevation (hoisting) is the behavior of JavaScript by default to elevate the current scope to the front.
Elevation (hoisting) is applied to declarations of variables and functions.
Therefore, a function can call before the declaration:

MyFunction (5);

function MyFunction (y) {return
  y * y;
}

You cannot elevate when you use an expression to define a function.
self-calling function
function expressions can be "called from".
The self invocation expression is invoked automatically.
If the expression is immediately followed by (), it is invoked automatically.
The declared function cannot be invoked from.
By adding parentheses, it shows that it is a function expression:

(function () {
  var x = "hello!!";   I will call myself
}) ();

Of course, you can also write as follows:

!function () {} ();
+function () {} ();
-function () {} ();
~function () {} ();
~ (function () {}) ();
void function () {} ();
(function () {} ());

The most common is the first method.

The above function is actually a function of anonymous self invocation (no functions name).
function can be used as a value
JavaScript functions are used as a value:

function MyFunction (A, b) {return
  a * b;
}

var x = MyFunction (4, 3);

A JavaScript function can be used as an expression:

function MyFunction (A, b) {return
  a * b;
}

var x = MyFunction (4, 3) * 2;

function is an object
Using the typeof operator to determine the function type in JavaScript returns "function".
But JavaScript functions describe an object as more accurate.
JavaScript functions have properties and methods.
The Arguments.length property returns the number of parameters received by the function call procedure:

function MyFunction (A, b) {return
  arguments.length
}

The ToString () method returns the function as a string:

function MyFunction (A, b) {return
  a * b;
}

var txt = myfunction.tostring ();

A function definition is an object's property, which is called an object method.
function if used to create a new object, called the object's constructor.

The following are sample code for various methods

 

Run as:
to invoke a generic method:
--------------------------------------------------------------------------------
Call constructor Method: Add1 (5,6)
One
--------------------------------------------------------------------------------
Call Function Direct Quantity method: Result (3,4)
7
Call function Direct measure: RESULT2 (3)
6
------------------------------------------------- -------------------------------
Function as data use
(2+3) + (4*5) =25
--------------------------------------------- -----------------------------------
Hello World

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.