Tuesday, December 24, 2019

Operator precedence (Thứ tự ưu tiên các tóan tử)

Operator precedence determines the way in which operators are parsed with respect to each other. Operators with higher precedence become the operands of operators with lower precedence.

Associativity

Associativity determines the way in which operators of the same precedence are parsed. For example, consider an expression:
a OP b OP c
Left-associativity (left-to-right) means that it is processed as (a OP b) OP c, while right-associativity (right-to-left) means it is interpreted as a OP (b OP c). Assignment operators are right-associative, so you can write:
a = b = 5;
with the expected result that a and b get the value 5. This is because the assignment operator returns the value that is assigned. First, b is set to 5. Then the a is also set to 5, the return value of b = 5, aka right operand of the assignment.

Examples

3 > 2 && 2 > 1
// returns true

3 > 2 > 1
// returns false because 3 > 2 is true, and true > 1 is false
// Adding parentheses makes things clear: (3 > 2) > 1

Table

The following table is ordered from highest (21) to lowest (1) precedence.
PrecedenceOperator typeAssociativityIndividual operators
21Groupingn/a( … )
20Member Accessleft-to-right… . …
Computed Member Accessleft-to-right… [ … ]
new (with argument list)n/anew … ( … )
Function Callleft-to-right… ( … )
Optional chainingleft-to-right?.
19new (without argument list)right-to-leftnew …
18Postfix Incrementn/a… ++
Postfix Decrement… --
17Logical NOTright-to-left! …
Bitwise NOT~ …
Unary Plus+ …
Unary Negation- …
Prefix Increment++ …
Prefix Decrement-- …
typeoftypeof …
voidvoid …
deletedelete …
awaitawait …
16Exponentiationright-to-left… ** …
15Multiplicationleft-to-right… * …
Division… / …
Remainder… % …
14Additionleft-to-right… + …
Subtraction… - …
13Bitwise Left Shiftleft-to-right… << …
Bitwise Right Shift… >> …
Bitwise Unsigned Right Shift… >>> …
12Less Thanleft-to-right… < …
Less Than Or Equal… <= …
Greater Than… > …
Greater Than Or Equal… >= …
in… in …
instanceof… instanceof …
11Equalityleft-to-right… == …
Inequality… != …
Strict Equality… === …
Strict Inequality… !== …
10Bitwise ANDleft-to-right… & …
9Bitwise XORleft-to-right… ^ …
8Bitwise ORleft-to-right… | …
7Nullish coalescing operatorleft-to-right… ?? …
6Logical ANDleft-to-right… && …
5Logical ORleft-to-right… || …
4Conditionalright-to-left… ? … : …
3Assignmentright-to-left… = …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
2yieldright-to-leftyield …
yield*yield* …
1Comma / Sequenceleft-to-right… , …

Tuesday, August 27, 2019

The JavaScript this Keyword

Example

var person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
Try it Yourself »

What is this?

The JavaScript this keyword refers to the object it belongs to.
It has different values depending on where it is used:
  • In a method, this refers to the owner object.
  • Alone, this refers to the global object.
  • In a function, this refers to the global object.
  • In a function, in strict mode, this is undefined.
  • In an event, this refers to the element that received the event.
  • Methods like call(), and apply() can refer this to any object.

this in a Method

In an object method, this refers to the "owner" of the method.
In the example on the top of this page, this refers to the person object.
The person object is the owner of the fullName method.
fullName : function() {
  return this.firstName + " " + this.lastName;
}
Try it Yourself »

this Alone

When used alone, the owner is the Global object, so this refers to the Global object.
In a browser window the Global object is [object Window]:

Example

var x = this;
Try it Yourself »
 In strict mode, when used alone, this also refers to the Global object [object Window]:

Example

"use strict";
var x = this;
Try it Yourself »

this in a Function (Default)

In a JavaScript function, the owner of the function is the default binding for this.
So, in a function, this refers to the Global object [object Window].

Example

function myFunction() {
  return this;
}
Try it Yourself »

this in a Function (Strict)

JavaScript strict mode does not allow default binding.
So, when used in a function, in strict mode, this is undefined.

Example

"use strict";
function myFunction() {
  return this;
}
Try it Yourself »

this in Event Handlers

In HTML event handlers, this refers to the HTML element that received the event:

Example

<button onclick="this.style.display='none'">
  Click to Remove Me!
</button>

Object Method Binding

In these examples, this is the person object (The person object is the "owner" of the function):

Example

var person = {
  firstName  : "John",
  lastName   : "Doe",
  id         : 5566,
  myFunction : function() {
    return this;
  }
};
Try it Yourself »

Example

var person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
Try it Yourself »
In other words: this.firstName means the firstName property of this (person) object.

Explicit Function Binding

The call() and apply() methods are predefined JavaScript methods.
They can both be used to call an object method with another object as argument.
You can read more about call() and apply() later in this tutorial.
In the example below, when calling person1.fullName with person2 as argument, this will refer to person2, even if it is a method of person1:

Example

var person1 = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
var person2 = {
  firstName:"John",
  lastName: "Doe",
}
person1.fullName.call(person2);  // Will return "John Doe"