Your Site Title

Javascript

Basics

Comments

// a one line comment

/* this is a longer,

Declarations

Variable scope

Variable hoisting

only its declaration is hoisted, but not its initialization

Data types

Data type conversion

Falsy values
false
undefined
null
0
NaN
the empty string ("")

Literals

Control flow and error handling

Loops and iteration

Functions

Recursion

The function's name
arguments.callee
An in-scope variable that refers to the function

Function parameters

Expressions and operators

Basic expressions

Numbers and dates

Numbers

In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE 754

Text formatting

Regular expressions

Collections

Key and value equality of Map and Set

Both the key equality of Map objects and the value equality of Set objects are based on the SameValueZero algorithm:

Equality works like the identity comparison operator ===.
-0 and +0 are considered equal.
NaN is considered equal to itself (contrary to ===).

Working with objects

Using object initializers

Objects created with initializers are called plain objects, because they are instances of Object, but not any other object type. Some object types have special initializer syntaxes — for example, array initializers and regex literals

const obj = {
  property1: value1, // property name may be an identifier
  2: value2, // or a number
  "property n": value3, // or a string
};

Using a constructor function

# 开头字母惯例大写
function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

const myCar = new Car("Eagle", "Talon TSi", 1993);

Using the Object.create() method

// Animal properties and method encapsulation
const Animal = {
  type: "Invertebrates", // Default value of properties
  displayType() {
    // Method which will display type of Animal
    console.log(this.type);
  },
};

// Create new animal type called animal1
const animal1 = Object.create(Animal);
animal1.displayType(); // Logs: Invertebrates

// Create new animal type called fish
const fish = Object.create(Animal);
fish.type = "Fishes";
fish.displayType(); // Logs: Fishes

Enumerating properties

Defining properties for all objects of one type

Car.prototype.color = "red";
console.log(car1.color); // "red"

Defining methods

objectName.methodName = functionName;

const myObj = {
  myMethod: function (params) {
    // do something
  },

  // this works too!
  myOtherMethod(params) {
    // do something else
  },
};

Using this for object references

this is a “hidden parameter” of a function call that’s passed in by specifying the object before the function that was called. For example, in Manager.sayHi() , this is the Manager object, because Manager comes before the function sayHi(). If you access the same function from another object, this will change as well. If you use other methods to call the function, like Function.prototype.call() or Reflect.apply(), you can explicitly pass the value of this as an argument.

Defining getters and setters

const myObj = {
  a: 7,
  get b() {
    return this.a + 1;
  },
  set c(x) {
    this.a = x / 2;
  },
};

console.log(myObj.a); // 7
console.log(myObj.b); // 8, returned from the get b() method
myObj.c = 50; // Calls the set c(x) method
console.log(myObj.a); // 25

Comparing objects

In JavaScript, objects are a reference type. Two distinct objects are never equal , even if they have the same properties. Only comparing the same object reference with itself yields true.

Class

Declaring a class

class MyClass {
  // Constructor
  constructor() {
    // Constructor body
  }
  // Instance field
  myField = "foo";
  // Instance method
  myMethod() {
    // myMethod body
  }
  // Static field
  static myStaticField = "bar";
  // Static method
  static myStaticMethod() {
    // myStaticMethod body
  }
  // Static block
  static {
    // Static initialization code
  }
  // Fields, methods, static fields, and static methods all have
  // "private" forms
  #myPrivateField = "bar";
}

# Private fields and methods are new features in classes with no trivial equivalent in function constructors.
function MyClass() {
  this.myField = "foo";
  // Constructor body
}
MyClass.myStaticField = "bar";
MyClass.myStaticMethod = function () {
  // myStaticMethod body
};
MyClass.prototype.myMethod = function () {
  // myMethod body
};

(function () {
  // Static initialization code
})();

Constructing a class

const myInstance = new MyClass();

Class declaration hoisting

Class expressions

const MyClass = class {
  // Class body...
};

Private fields

class Color {
  // Declare: every Color instance has a private field called #values.
  #values;
  constructor(r, g, b) {
    this.#values = [r, g, b];
  }
  getRed() {
    return this.#values[0];
  }
  setRed(value) {
    this.#values[0] = value;
  }
}

const red = new Color(255, 0, 0);
console.log(red.getRed()); // 255

Static properties

# Static methods 
static isValid(r, g, b) {
   return r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255;
}

Extends and inheritance

class Color {
  #values;
  constructor(r, g, b, a = 1) {
    this.#values = [r, g, b, a];
  }
  get alpha() {
    return this.#values[3];
  }
  set alpha(value) {
    if (value < 0 || value > 1) {
      throw new RangeError("Alpha value must be between 0 and 1");
    }
    this.#values[3] = value;
  }
}

class ColorWithAlpha extends Color {
  #alpha;
  constructor(r, g, b, a) {
    super(r, g, b);
    this.#alpha = a;
  }
  get alpha() {
    return this.#alpha;
  }
  set alpha(value) {
    if (value < 0 || value > 1) {
      throw new RangeError("Alpha value must be between 0 and 1");
    }
    this.#alpha = value;
  }
}

Iterators and generators

Generator functions

function* 

Meta programming

Reference