This article mainly introduces how to deeply understand the objects in JavaScript. It is the basic knowledge in getting started with JavaScript. If you need them, you can refer to JavaScript as an Object-Oriented Programming (OOP) language. A programming language can be called object-oriented. It provides developers with four basic functions:
- Encapsulation-stores relevant information, whether it is data or method or object
- Aggregation-store one object to another object
- Inheritance-the ability of a class depends on another class (or number of classes), used for its partial attributes and Methods
- Polymorphism-compile functions or methods to work in different ways
The object is a property. If an attribute contains a function, it is considered an object method. Otherwise, this attribute is considered an attribute.
Object Property:
The attributes of an object can be any three basic data types, or any abstract data type, such as another object. An object attribute is usually a variable of the internal object method, but it can also be a variable that is globally visible throughout the page.
The syntax used to add attributes is:
objectName.objectProperty = propertyValue;
Example:
The following is a simple example to illustrate how to obtain the document title by using the attributes of the "title" file object:
var str = document.title;
Object method:
The method is to let the object do something. The difference between a function and a method is that an independent unit and method of a function statement is appended to an object, and there is little difference between them that can be referenced by this keyword.
This method can be used to perform complex mathematical operations on a set of local attributes and parameters from the display object's screen content.
Example:
The following is a simple example to illustrate how to use the write () Document Object method to write anything in the document:
document.write("This is test");
User-defined objects:
The descendant of all user-defined objects and built-in objects called objects.
New OPERATOR:
The new operator is used to create an instance of an object. To create an object, the new operator is followed by the constructor.
In the following example, the constructor Object (), Array (), and Date ().. These constructors are built-in JavaScript Functions.
var employee = new Object();var books = new Array("C++", "Perl", "Java");var day = new Date("August 15, 1947");
Object () constructor:
Constructor is a function used to create and initialize objects. JavaScript provides a special constructor that calls Object () to construct objects. The returned value constructed by Object () is assigned to a variable.
A variable contains a reference to a new object. The property assigned to this object is a constant and is not defined using the var keyword.
Example 1:
This example demonstrates how to create an object:
User-defined objects