JS Basic knowledge: expressions

Source: Internet
Author: User

First, what is an expression?

I understand the expression: when a program executes to 1 expressions, it returns 1 values to the location of the expression.

var a = ten, B = +;  // This is the initialization variable Console.log (a//  ) calculates the value of the variable A, B (identifier resolution), and then returns the value of both to its location. Then it is equivalent to Console.log (10+20)

Ii. several common expressions

(i) original expression


1. Direct volume (literal)

// Digital // string

Reserved words for 2.JavaScript


"Reserved words" are identifiers that have special meanings in the program, and programmers cannot use them as variables.

true // Boolean value  This // call context for a function


3. Variables

var // calculates the value of variable A, returns a 10, and then passes 10 into the alert function. 


(b) An array expression, an object expression



An array expression is a newly created array.

// creates an array of 1. 


An object expression, like an array expression, is a new 1 object.

// Create 1 Objects


(c) Function expression

Create 1 functions, either in the form of a function declaration or as a function expression. A function expression is to create 1 functions and save its references in 1 variables.

var function return // Create 1 Anonymous functions whose references are stored in the variable foo. 


(iv) Property access expressions

There are 2 types of syntax for property-access expressions:

1. "Expression. Identifier": "Expression" requires 1 objects, and "identifier" must be an attribute that already exists for the object.

var // obj.x returns the X attribute in the Obj object, 1. 

2. Expression [expression]: The 1th expression also needs to be 1 objects, and the value of the 2nd expression can be:

(1) String: Then this is the object's property access or property setting.

Property access to the object:

var obj = {x:1, y:2};  // accesses the value of the X property in the Obj object, returning 1. 

object's property setting (assigning values to 1 properties):

var // {x=1,  y=2,  z=3}


(2) Number: array element access.

var // accesses an element with an index of 0 in the array and returns 1. 

(v) Function call expression

The function call expression is this: "Function name (argument)". The return value of the function call expression, depending on the return statement.

1 did not write return statement: function call returned undefined.

    function Add (x) {            x+x;     }    Console.log (the Add (///  function does not have a return statement, the function call returns undefined.) 


2. The return statement was written, but the returned value was not written: The function call returned undefined.

    function Add (x) {            x+x;              return ;    }    Console.log (Add (// ) writes the return statement but does not return a value, the function call returns undefined. 


3. Write the return statement and the expression to return: Returns the value of the expression.

    function Add (x) {        return x+x;     }    Console.log (Add (//  ) writes the return statement, specifying the returned value (x+x), so the function call returns 2. 

Resources:

JavaScript authoritative Guide (6th edition) 4.1-4.6

JS Basic knowledge: expressions

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.