JavaScript language essence note 2 - inheritance, array, regular expression

  • content {:toc}

Take notes of reading butterfly book. This chapter is the notes of the following chapters in the book: inheritance, array and regular expression.

inherit

Two major benefits of inheritance: code reuse and the introduction of a set of type system specifications.

Pseudo class

JavaScript generates objects through constructor functions.

The constructor calls the pattern, that is, to call a function with the prefix new.

var Mammal = function(name) {
    this.name = name
}

Mammal.prototype.getName = function() {
    return this.name
}

Mammal.prototype.says = function() {
    return this.saying || ''
};

var myMammal = new Mammal('Herb')
console.log(myMammal.getName()) //Herb

It's not recommended in the book. There are many risks. If you forget to add the new prefix, this cannot be bound to a new object. Instead, it is bound to the global object and destroys the global variable environment.

Object Specifiers

The constructor in the previous section may accept a large list of parameters. We can write this:

var myObject = Maker({
    first: f,
    middle: m,
    last: l,
    state: s,
    city: c
})

Pass the JSON object to the constructor, and it returns a fully constructed object.

prototype

In a pure prototype pattern, we will abandon classes and focus on objects instead. A new object can inherit the properties of an object.

var myMammal = {
    name: 'MM',
    getName: function() {
        return this.name
    },
    says: function() {
        return this.saying || ''
    }
}

var myCat = Object.create(myMammal)
myCat.name = 'Kitty'
myCat.saying = 'meow'
myCat.run = function() {
    return 'Kitty is running'
}
myCat.getName = function() {
    return this.says + ' ' + this.name + ' ' + this.says
}

This is a differentiated inheritance.

Functionalization

The inheritance model seen earlier does not protect privacy. All properties of the object are visible. Cannot get private variables and private functions. In order to solve this problem, we have a module mode.

Constructing a function that generates objects requires four steps:

  1. Create a new object.
  2. Selectively define private variables and methods.
  3. Extend the method to this new object.
  4. Returns the new object.
var mammal = function(spec) {
    var that = {}

    that.getName = function() {
        return spec.name
    }
    that.says = function() {
        return spec.saying || ''
    }

    return that
}

var myMammal = mammal({
    name: 'Herb',
    saying: 'Cheers!'
})

console.log(myMammal.getName()) //Herb
console.log(myMammal.says()) //Cheers!

You can also refer to the previous article, JavaScript language essence notes 1 - module part of syntax, object and function.

parts

I don't particularly understand this part. I want to see it after I finish learning the classes and modules in ES2015.

array

array literal

An array literal is an expression that encloses zero or more comma separated values in square brackets.

In most languages, multiple elements of an array are required to be of the same type. JavaScript allows arrays to contain values of any mixed type.

length

The length attribute of a JavaScript array has no upper bound. If an element is stored with a number greater than or equal to the current length as the subscript, the length value will be increased to accommodate the new element, and the array out of bounds error will not occur.

delete

Arrays are also objects. You can delete elements with delete

var numbers = ['one', 'two', 3, 'four', 'wu']

delete numbers[0]
console.log(numbers[0]) //undefined
console.log(numbers.length) //5

You can use the splice method to delete and modify.

numbers.splice(0, 1)
console.log(numbers[0]) //two
console.log(numbers.length) //4

enumeration

Use the regular for loop to ensure the order of the array.

Confusing places

When the property name is a small and continuous integer, you should use an array, otherwise use an object.

console.log(typeof [1, 2]) //object

The type of the returned array is object, which has no meaning.

Method for judging array type

console.log(Array.isArray(numbers)) //true

Methods in ECMAScript 5.1 (ECMA-262) and ECMAScript 2015 (6th Edition, ECMA-262).

Or the following method.

var is_array = function(value) {
    return Object.prototype.toString.apply(value) === '[object Array]'
}
console.log(is_array(numbers)) //true

method

Array methods are stored in array Functions in prototype.

Array is an object, so array Prototype is also extensible.

Specify initial value

JavaScript arrays do not prefabricate values.

JavaScript does not have multidimensional arrays. Like most C-like languages, it supports arrays whose elements are arrays.

var matrix = [
    [7, 8, 9],
    [4, 5, 6],
    [1, 2, 3]
]
console.log(matrix[1][2]) //6

regular expression

Regular expressions have been written more in previous blogs. For details, see:

Baidu Institute of Web front end technology (2) - regular expression part of JavaScript foundation 1

Baidu Institute of Web front end technology (2) - regular expression part of JavaScript foundation 2

On grouping and reference in regular expressions

Added by phpwiz on Wed, 02 Mar 2022 14:42:04 +0200