Basic knowledge of literal usage and function in JavaScript

Source: Internet
Author: User
Tags anonymous function definition

JavaScript literal volume
In a programming language, a literal is a constant, such as 3.14.
The number literal can be either an integer or a decimal, or a scientific count (e).

3.14

1001

123e5

string literal can use either single or double quotation marks to be written with double or a quotes:

John Doe '

John Doe '

An expression literal is used to calculate:

5 + 6

5 * 10

Array literal defines an array:

[40, 100, 1, 5, 25, 10]


Object literal defines an object:

{firstName: "John", LastName: "Doe", Age:50, Eyecolor: "Blue"}


function literal to define a function:

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

JavaScript function definition
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;
}

The semicolon is used to separate executable javascript statements.
Because a function declaration is not an executable statement, it does not end with a semicolon.

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.
Note the above function ends with a semicolon because it is an execution statement.

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 ()).
Instance

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:
Instance

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

var x = MyFunction (4, 3);


Note In JavaScript, a lot of times, you need to avoid using the new keyword.

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.
Y cannot call a declared function.
By adding parentheses, it shows that it is a function expression:
Instance

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

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:
Instance

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

var x = MyFunction (4, 3);

A JavaScript function can be used as an expression:
Instance

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".
However, JavaScript functions are described as more accurate as an object.
JavaScript functions have properties and methods.
The Arguments.length property returns the number of parameters received by the function call procedure:
Instance

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


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

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

var txt = myfunction.tostring ();


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.