Home > Article > Web Front-end > JavaScript minimalist introductory tutorial (2): objects and functions_javascript skills
Reading this article requires programming experience in other languages.
Simple types in JavaScript include:
1. Numbers
2. String
3.Boolean (true and false)
4.null
5.undefined
All other types are objects (let’s not be fooled by the return value of the typeof operator), for example:
1. Function
2.Array
3. Regular expression
4. Objects (objects are naturally objects)
Object Basics
In JavaScript, an object is a collection of attributes (an object is an associative array). Each attribute includes:
1. Attribute name, must be a string
2. Attribute value, which can be any value except undefined
Create an object from an object literal:
Attribute name and attribute value of the object:
If the attribute name is a legal identifier, the quotes can be omitted:
Let’s look at an example of property access:
If the attribute name is not a legal identifier, it needs to be wrapped in quotes. A property that does not exist has a value of undefined. Objects are passed by reference rather than by value:
Here x and owner refer to the same object.
The properties of an object can be deleted using the delete operator:
Prototype of the object
Each object is linked to a prototype object, and objects can inherit properties from the prototype object. We create an object through object literal, and its prototype object is Object.prototype object (Object.prototype object itself has no prototype object). When we create an object, we can set the prototype object of the object (we will discuss the specific setting method later). When trying to get (rather than modify) a property of an object, if the property does not exist on the object, JavaScript will try to get the property from the prototype object of the object. If the property does not exist in the prototype object, then from the prototype object The prototype object is searched, and so on, until the Object.prototype prototype object. Compared with getting attributes, when we modify a certain attribute of the object, it will not affect the prototype object.
Function Basics
In JavaScript, functions are also objects, which are linked to the Function.prototype prototype object (Function.prototype is linked to Object.prototype). The function has a property named prototype, and the type of its value is an object. This object has a property constructor, and the value of the constructor is this function:
Functions are objects. You can use functions just like objects. That is to say, functions can be saved in variables or arrays, passed to functions as parameters, and functions can be defined inside functions. As a side note, functions have two hidden properties:
1. Function context
2. Function code
The function is created as follows:
The function name after the keyword function is optional. We formulate the function name mainly for several purposes:
1. For recursive calls
2. Used by debuggers, development tools, etc. to identify functions
Many times we don’t need a function name. A function without a function name is called an anonymous function. The parameter list is enclosed in parentheses. JavaScript does not require actual parameters and formal parameters to match, for example:
If there are too many actual parameters, the extra actual parameters will be ignored. If there are too few actual parameters, the value of the unassigned formal parameter will be undefined. The function must have a return value. If the return value is not specified through the return statement, the function return value is undefined.
A function and the external variables it accesses form a closure. This is the key beauty of JavaScript.
Function call
When each function is called, it will receive two additional parameters:
1.this
2.arguments
The value of this is related to the specific calling mode. There are four calling modes in JavaScript:
1. Method calling mode. If a property of an object is a function, it is called a method. If a method is called via o.m(args), this is the object o (it can be seen that this and o are only bound when the call is made), for example:
2. Function calling mode. If a function is not a property of an object, then it will be called as a function, and this will be bound to the global object, for example:
This behavior is sometimes confusing. Let’s look at an example:
The desired result should be:
3. Constructor calling mode. Functions intended to be prefixed with new are called constructors, for example:
A function can be called by adding new in front of it (such functions usually start with a capital). After adding new, an object linked to the prototype property of this function will be created, and this in the constructor will be this object.
4.apply calling mode. The apply method of the function is used to call the function, which has two parameters, the first is this, and the second is the parameter array, for example:
When the function is called, we can access a class array named arguments (not a real JavaScript array), which contains all the actual parameters, so that we can implement variable length parameters:
Exception
Now let’s talk about JavaScript’s exception handling mechanism. We use the throw statement to throw exceptions and the try-cache statement to catch and handle exceptions:
Add properties to JavaScript types
Constructors exist for most types in JavaScript:
1. The constructor of the object is Object
2. The constructor of the array is Array
3. The constructor of the function is Function
4. The constructor of string is String
5. The constructor of numbers is Number
6. The constructor of Boolean is Boolean
7. The constructor of regular expression is RegExp
We can add properties (often methods) to the prototype of the constructor to make this property available to related variables:
Scope
JavaScript requires functions to build scope:
An anonymous function is created and executed here. You can hide variables you don’t want to expose through scope:
Inherit
There are many ways to implement inheritance in JavaScript.
When creating an object, we can set the prototype object associated with the object. We do this:
The Object.create method is defined in ECMAScript 5. If you use ECMAScript 3, you can implement a create method yourself:
Through the Object.create method, we perform prototype-based inheritance: a new object directly inherits the properties of an old object (compared to class-based inheritance, there is no need for the existence of a class, and the object directly inherits the object). Example:
The above code is very simple, but it cannot protect private members. We can use module pattern. In the module pattern, a certain type of object is generated by a function, and the function scope is used to protect private members from external access:
In the module pattern, inheritance is achieved by calling constructors. In addition, we can also access the methods of the parent class in the subclass: