js expression of JavaScript basic syntax

  • 2021-06-28 08:33:06
  • OfStack

The javascript expression, which can be divided into original and complex expressions, is described in detail in this paper.Generally, the terms that people hear more about javascript basic grammar are operators and statements.However, there is actually another term that is often used, but rarely mentioned, that is the javascript expression (expression).

Original expression (primary exression)

The original expression is the smallest unit of the expression -- it no longer contains other expressions.The original expressions in javascript include this keywords, identifier references, literal references, array initialization, object initialization, and grouping expressions


PrimaryExpression : 
this 
Identifier 
Literal 
ArrayLiteral 
ObjectLiteral 
( Expression ) 

this keywords and identifiers

this; //Return current object

i; //Returns the value of the variable i

sum; //Returns the value of the variable sum

Literal

Literal quantity (literal), translated as direct quantity, is the data value used directly in the program


Literal ::
NullLiteral
BooleanLiteral
NumericLiteral
StringLiteral 
RegularExpressionLiteral 
null;
undefined;
true;
false;
1;
'abc';
/pattern/; 

Array and object initialization

Array initialization and object initialization are actually an initialization process described literally.These two initialization expressions are sometimes called Object Direct Quantity and Array Direct Quantity.


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

group expression

Grouping expressions are essentially parentheses, which override the precedence of operators

Complex expression (MemberExpression)

Complex expressions are a combination of original expressions and operators (operator), including attribute access expressions, object creation expressions, and function expressions


MemberExpression : 
MemberExpression [ Expression ] 
MemberExpression . IdentifierName 
new MemberExpression Arguments
FunctionExpression 

Property Access Expression

The attribute access expression operation yields the value of an object attribute or an array element. javascript defines two syntaxes for attribute access


MemberExpression . IdentifierName 
MemberExpression [ Expression ] 

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

The second way to write is to use square brackets, which contain another expression (this method works for objects and arrays).The second expression specifies the name of the property to be accessed or the index representing the array element to be accessed


var o = {x:1,y:{z:3}}; // Object Literal 
var a = [o,4,[5,6]]; //  Array Literal Quantity Containing Objects 
o.x;// Expression o Of x attribute 
o.y.z;// Expression o.y Of z attribute 
o['x'];// object o Of x attribute 
a[1];// Expression a Medium index is 1 Elements of  

Regardless of the form of attribute access expression used, expressions before'. 'and'[' are always evaluated first

If the result of the calculation is null or undefined, the expression throws a type error exception because neither value can contain any attributes

If the result of calculation is not an object, javascript will convert it to an object

If an object expression follows a period and an identifier, the attribute value specified by that identifier is found and returned as the value of the entire expression

If an object expression is followed by a pair of parentheses, the value of the expression within the square brackets is calculated and converted to a string

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

Object Creation Expression

Object Creation Expression Creates an object and calls a function to initialize the properties of the new object


new Object();
new Point(2,3); 

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

The function definition expression defines an javascript function whose value is the newly defined function

A typical function definition expression contains the keyword function followed by a pair of parentheses, a comma-separated list of zero or more identifiers (parameter names), followed by a curly bracketed list

javascript Code Snippet (Function Body)


function square(x){
return x*x;
} 

A function definition expression can also contain the name of a function, or a function can be defined by a function statement, not a function expression


var square = function(x){return x*x;} 

A function call expression is a syntax representation for calling or executing a function or method.If the expression is a property access expression, then this call is called a method call


f(0);
Math.max(x,y,z);
a.sort();

Related articles: