1. The nature of classes

Class is still fucntion

We can simply think of a class as just another way of writing a constructor

Constructors have several characteristics:

  1. The constructor has the prototype object prototype
  2. The constructor prototype object has constructor pointing to the constructor itself
  3. Constructors can add methods through prototype objects
  4. The constructor creates an instance object that has a __proto__ prototype pointing to the constructor’s prototype object

As it turns out, a class is just another way of writing a constructor

The new class writing just makes the writing of object prototypes clearer and more like the syntax of object-oriented programming

So ES6 classes are syntactic sugar, and syntactic sugar is a convenient way to write.

2. New method Overview in ES5

There are some new methods in ES5 that can easily manipulate arrays or strings. These methods include:

  • Array methods
  • String method
  • Object methods

2.1 Array Methods

Iterating (traversing) methods: forEach(),map(),filter(),some(),every();

The foreach () method

The map () method

The map() method creates a new array with the result that each element in the array is the return value from a call to the provided function.

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

Copy the code

The filter () method

The filter () method creates a new array. The elements in the new array are filtered by checking all elements in the specified array. It returns a new array directly

Some () method

Checks whether an element in an array meets a specified condition. The common point is to find if there are elements in the array that meet the criteria

Note that it returns a Boolean value, true if the element is found, false if not.

If the first element that satisfies the condition is found, the loop is terminated and the search is not continued.

Every () method

The every() method tests whether all elements in an array pass the test of a given function. It returns a Boolean value.

Note: If an empty array is received, this method returns true in all cases.

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true

Copy the code

Note:

Difference between foreach and some

  1. A return does not terminate an iteration in foreach
  2. A return in some terminates the iteration, which is more efficient
  3. A return does not terminate an iteration in a filter

2.2 String Methods

The trim() method removes whitespace from both ends of a string

str.trim()
Copy the code

The trim() method does not affect the original string itself; it returns a new string

        var str = "   chengzi  "
        var str1 = str.trim()
        console.log(str1);
Copy the code

Note: the trim method does not remove the middle whitespace; it only removes the Spaces at both ends

2.4 Object Methods

The Object. DefineProperty () defines new attributes of the object or modifies existing attributes

Object.defineProperty(obj,prop,descriptor)
Copy the code
  • Obj: required. The target object
  • Prop: required. The name of the property that you want to define or modify
  • Descriptor: required. Properties owned by the target property

The third parameter descriptor is specified as an object {}

  • Value: Sets the value of the property. The default value is undefined
  • Writable: Indicates whether the value can be rewritten. True | false is false by default
  • Enumerable: Whether a target property can be enumerable. True | false is false by default
  • Configurable: whether the target attribute can be deleted or whether to modify the characteristics of true | false is false by default