JavaScript basic syntax JS expression _javascript Skills

Source: Internet
Author: User
Tags function definition

This article details the JavaScript expressions, which are divided into original expressions and complex expressions. Generally speaking, the basic grammar of JavaScript, people listen to a lot of terms are operators and statements. However, there is actually a term used frequently, but rarely mentioned, and this is the JavaScript expression (expression).

Original expression (primary exression)

The original expression is the smallest unit of an expression-it no longer contains other expressions. The original expressions in JavaScript include this keyword, identifier reference, literal reference, array initialization, object initialization, and grouping expressions

Primaryexpression: 
This 
Identifier 
Literal 
arrayliteral 
objectliteral 

This keyword and identifier

this;//returns the current object

i;//returns the value of the variable i

sum;//returns the value of the variable sum

Literal amount

Literal (literal), translated into direct volume, is the data value directly used in the program

Literal::
nullliteral
booleanliteral
numericliteral
stringliteral 
Regularexpressionliteral 
null;
undefined;
true;
false;
1;
' ABC ';

Array and object initialization

Array initialization and object initialization are actually a process of initialization that is described in literal terms. These two initialization expressions are sometimes called "Object Direct Quantity" and "Array Direct quantity"

[];
[1,2,3];
{};

grouping expressions

A grouping expression is actually a bracket that overrides the precedence of an operator

Complex expression (memberexpression)

Complex expressions are composed of the original expression and the operator (operator), including property access expressions, object-creation expressions, and function expressions

Memberexpression: 
memberexpression [Expression] 
memberexpression. IdentifierName 
New Memberexpression Arguments

Property access Expression

A property access expression operation can get the value of an object property or an array element, and JavaScript defines two syntaxes for property access

Memberexpression. IdentifierName 

The first type is an expression followed by a period and an identifier. An expression specifies the object, and the identifier specifies the name of the property that needs to be accessed

The second way to do this is to use square brackets, which is a different expression in square brackets (this applies to objects and arrays). The second expression specifies the name of the property to be accessed or the index that represents the element to access the array

var o = {X:1,y:{z:3}}; Object Literal
var a = [o,4,[5,6]];//contains the object's array literal
o.x;//expression O's X property
o.y.z;//expression o.y Z property
o[' x '];//object O's X property

Regardless of which form of property access expression is used, the '. ' and ' [' expressions are always evaluated first.

If the evaluation result is null or undefined, the expression throws a type error exception because neither of these values can contain any properties

If the result of the calculation is not an object, JavaScript converts it to an object

If an object expression follows a period and an identifier, it looks for the property value specified by the identifier and returns it as the value of the entire expression

If an object expression follows an opposite bracket, the value of the expression within the square brackets is evaluated and converted to a string

In either case, if the named property does not exist, then the value of the entire property access expression is undefined

Object-creation Expression

Object creation expression creates an object and invokes a function to initialize the properties of the new object

New Object ();

This pair of empty parentheses can be omitted if an object creation expression does not need to pass in any arguments to the constructor.

New Object;

function expression

function expressions are divided into function-definition expressions and function-call expressions

A function definition expression defines a JavaScript function, and the value of the expression is the newly defined function

A typical function definition expression contains the keyword functions, followed by a pair of parentheses, a comma-delimited list in parentheses, and a list of 0 or more identifiers (parameter names) followed by a column wrapped with curly braces.

JavaScript code snippet (function body)

function Square (x) {return
x*x;

A function definition expression can also contain the name of a function, which can be defined by a function statement rather than a function expression

 
 

A function call expression is a syntax representation of a function or method that is invoked or executed. If this expression is a property access expression, then this call is called the method call

F (0);
Math.max (x,y,z);
A.sort ();

The above is a small set to introduce JavaScript basic grammar of the JS expression of the relevant content, I hope to help. If you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.