Explanation of javascript Expressions and Operators

  • 2021-07-18 06:28:23
  • OfStack

1. js expression

Definition: An expression refers to any available program unit that can calculate the value. --wiki

The expression is an js1 phrase, but the js interpreter is used to generate 1 value. --js "Authoritative Guide"

1. The original expression.

Constant/Direct Quantity: 3.14/"test" etc

Keywords: null, this, true, false, etc

Variables: i, k, j, etc

Compound expression: The original expression and the original expression can be connected by operators to form a compound expression.

For example: 10 * 20 This is a compound expression

2. Initialization expressions (of arrays, objects).

For example:

[1, 2] Equivalent to new Array (1, 2);

[1,,, 2] is equivalent to new Array (1, undefined, undefined, 2);

{x: 1, y: 2} is equivalent to var obj = new Object ();

obj.x = 1;obj.y = 2;

3. Function expressions.

var fn = function(){};

Or:

(function(){ console.log("hello") })();

If you don't understand the function expression, there will be a corresponding introduction later.

4. Property access expression.

var obj = {x:1}

We can access its properties through obj. x or obj ["x"], and this class 1 expression is called a property access expression.

5. Invoke the expression.

func (); To call a function, such an expression is called a call expression.

6. Object creation expression.

For example:

new Func (1, 2); We can pass parameters, and if there are no parameters, we can also new Object; It is also legal to create an empty object.

2. js operator (1 is generally used to perform 1 operation between expressions).

1. By the number of operands of the operator:

1 yuan operator: + num

2 yuan operator: a + b

3 yuan operator: c? a: b

2. Distinguish by function:

Assignment: x += 1 (or -=, =, etc.)

Comparison: a = = b

Arithmetic: a-b

Bit: a b

Logic: exp1 & & exp2 (OR NAND)

String: "a" + "b"

Special: delete obj. x

Let's talk about special operator expansion:

Conditional operator: var c = true? 1: 2//c=1

Comma Operator: var val = (1, 2, 3)//val = 3 Calculated from left to right, taking the last

delete operator: Is to delete the property on the object.

--For example:


var obj = {x : 1}
    obj.x //1
    delete obj.x
    obj.x //undefined

Starting with IE9, we can set the configurable tag on the object, and the attribute can be dropped by delete when configurable: true

Example:


var obj = {}
Object.defineProperty(obj, 'x', {
configurable:false,
value:1
});
delete obj.x
obj.x //1 

3. in operator.

Example: window. x = 1; Then we want to know if window has x, so we can use in

”x“ in window;    // true  

4. instanceof and typeof were described in detail in the previous chapter. If you forget it, you can look at it.

5. new operator

For example, let's create a function constructor: function Foo () {}

  Foo.prototype.x = 1;

We can use the new operator to create an object obj

  var obj = new Foo();

Thus obj. x; //1 We can get 1 from the prototype property on its constructor

We can use obj. hasOwnProperty ("x"); //false to determine whether this attribute is on the object or on the prototype of the object

obj._proto_ Get its prototype, obj._proto_. hasOwnProperty ("x"); //true description is on-prototype

6. this operator.


        this ;  //window( Browser )
        var obj = {
        func:function(){
    return this;
   }
 }
obj.func(); // obj

The this operator is also quite special, which we will talk about in detail later.

7. void operator.


   void 0 //undefined
 void (0) //undefined

void returns undefined regardless of the subsequent operands.

8. The priority of operators is also recommended that you look at 1, which is more helpful for you to read complex expressions.


Related articles: