This article mainly introduces the basic usage of literal and function in JavaScript, including the concepts of constructor and self-called function. For more information, see
JavaScript literal
In programming languages, a literal is a constant, such as 3.14.
Number can be an integer, a decimal Number, or a scientific count (e ).
3.141001123e5
Strings can be enclosed in single quotes or double quotes:
"John Doe"'John Doe'
The expression literal is used for calculation:
5 + 65 * 10
An Array (Array) literally defines an Array:
[40, 100, 1, 5, 25, 10]
The Object literally defines an Object:
{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}
The Function literally defines a Function:
function myFunction(a, b) { return a * b;}
JavaScript function definition
JavaScript uses the keyword function to define a function.
A function can be defined through declaration or an expression.
Function Declaration
In the previous tutorial, you have learned the syntax of function declaration:
Function functionName (parameters) {executed code}
Function declaration will not be executed immediately and will be called as needed.
Instance
function myFunction(a, b) { return a * b;}
The Note Semicolon is used to separate executable JavaScript statements.
Function declaration is not an executable statement, so it does not end with a semicolon.
Function expression
JavaScript functions can be defined using an expression.
Function expressions can be stored in variables:
Instance
var x = function (a, b) {return a * b};
After 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 ).
A function is stored in a variable. A function name is usually called by a variable name.
Note: The preceding function ends with a semicolon because it is an execution statement.
Function () constructor
In the above example, we know that a function is defined by the 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 instance can be written:
Instance
var myFunction = function (a, b) {return a * b}var x = myFunction(4, 3);
Note in JavaScript, you often need to avoid using the new keyword.
Hoisting)
We have learned about hoisting in the previous tutorial )".
Hoisting is the behavior in which JavaScript promotes the current scope to the previous step by default.
Hoisting is used to declare variables and functions.
Therefore, the function can be called before declaration:
myFunction(5);function myFunction(y) { return y * y;}
Function Definition using expressions cannot be upgraded.
Self-called Function
Function expressions can be "self-called ".
The self-called expression is automatically called.
If the expression follows (), it is automatically called.
Y cannot self-call declared functions.
By adding parentheses, it is a function expression:
Instance
(Function () {var x = "Hello !! "; // I will call myself })();
The above function is actually an anonymous self-called function (no function name ).
A function can be used as a value.
The JavaScript function is used as a value:
Instance
function myFunction(a, b) { return a * b;}var x = myFunction(4, 3);
JavaScript functions can be used as expressions:
Instance
function myFunction(a, b) { return a * b;}var x = myFunction(4, 3) * 2;
The function is an object.
Using the typeof operator in JavaScript to determine the function type will return "function ".
However, JavaScript function descriptions are more accurate to an object.
JavaScript functions have attributes and methods.
The arguments. length attribute returns the number of parameters received during the function call process:
Instance
function myFunction(a, b) { return arguments.length;}
The toString () method returns a function as a string:
Instance
function myFunction(a, b) { return a * b;}var txt = myFunction.toString();