Home >Web Front-end >Front-end Q&A >What are the three types of JavaScript objects?

What are the three types of JavaScript objects?

青灯夜游
青灯夜游Original
2021-11-24 18:17:025946browse

The three object types of JavaScript: 1. Internal objects, including local objects that need to be instantiated to be used and built-in objects that can be used without instantiation; 2. Host objects, which are the environments in which JS scripts are executed. Provided objects; 3. Custom objects are objects defined by developers themselves.

What are the three types of JavaScript objects?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Objects in JavaScript can be divided into three major categories, namely internal objects (local objects and built-in objects), host objects and custom objects.

1, internal objects

1), local objects, objects provided by ECMAScript that need to be instantiated (new) before they can be used:

Object, Function, Array, String, Boolean, Number, Date, RegExp, Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError

2), built-in objects , objects provided by ECMAScript that can be used without instantiation:

Only Global (global object) and Math

Math object properties

Properties Description
E Returns the arithmetic constant e, which is the base of the natural logarithm (approximately equal to 2.718) .
LN2 Returns the natural logarithm of 2 (approximately 0.693).
LN10 Returns the natural logarithm of 10 (approximately 2.302).
LOG2E Returns the base 2 logarithm of e (approximately 1.4426950408889634).
LOG10E Returns the base 10 logarithm of e (approximately 0.434).
PI Returns pi (approximately equal to 3.14159).
SQRT1_2 Returns the reciprocal of the square root of 2 (approximately equal to 0.707).
SQRT2 Returns the square root of 2 (approximately 1.414).

Math Object Method

##MethodDescriptionabs(x)Returns the absolute value of x. acos(x)Returns the arc cosine of x. asin(x)Returns the arcsine of x. atan(x)Returns the arctangent of x as a number between -PI/2 and PI/2 radians. atan2(y,x)Returns the angle from the x-axis to the point (x,y) (between -PI/2 and PI/2 radians between). ceil(x) Round the number up. cos(x)Returns the cosine of the number. exp(x)Returns the exponent of Ex. floor(x)Round x down. log(x)Returns the natural logarithm of the number (base is e). max(x,y,z,...,n)Returns the highest value among x,y,z,...,n. min(x,y,z,...,n)Returns the lowest value among x,y,z,...,n. pow(x,y)Returns x raised to the y power. random()Returns a random number between 0 ~ 1. round(x)Rounding. sin(x)Returns the sine of the number. sqrt(x)Returns the square root of the number. tan(x)Returns the tangent of the angle.

JavaScript Global Properties

PropertiesDescription Infinity represents a positive infinity value. NaNIndicates whether a value is a numeric value. undefinedIndicates an undefined value.

JavaScript global function

Function Description
decodeURI() Decode an encoded URI.
decodeURIComponent() Decode an encoded URI component.
encodeURI() Encode a string into a URI.
encodeURIComponent() Encode a string into a URI component.
escape() Encode the string.
eval() Evaluates a JavaScript string and executes it as script code.
isFinite() Check whether a value is a finite number.
isNaN() Checks whether a value is a number.
Number() Convert the value of the object to a number.
parseFloat() Parse a string and return a floating point number.
parseInt() Parse a string and return an integer.
String() Convert the value of the object to a string.
unescape() Decode the string encoded by escape().

2. Host object

The host object is the object provided by the environment that executes the JS script. Object provided by the browser. All BOM and DOM are host objects.

3. Custom objects

Objects defined by developers

⑴Object literal method (via JSON Creating objects)

Disadvantages: Using the same interface to create many objects will produce a lot of duplicate code.

⑵Factory mode.

①Factory pattern is to put the statement of creating an object in a function, create a specific object by passing in parameters, and finally return the created object.

The function createPerson() can construct a Person object containing all necessary information based on the parameters received.

This function can be called countless times, and each time it will return an object containing 2 properties and a method.

②Disadvantages: Although the factory pattern can create multiple similar objects, it cannot solve the problem of object identification, that is, how to know the type of an object.

⑶Constructor pattern

①Disadvantages: The main problem with using constructors is that each method must be created on each instance.

②In ECMAScript, functions are objects, so every time a function is defined, an object is instantiated.

③In other words, the methods of multiple objects instantiated through the constructor are multiple different methods, but their internal codes and implemented functions are the same, which results in a certain waste of resources. .

⑷Prototype pattern

①In js, each function has a prototype attribute, which is a pointer pointing to an object, called a prototype object.

②Using the prototype pattern allows all instances to share the properties and methods in the prototype object, that is, there is no need to define the information of the object instance in the constructor.

③Disadvantage: The link of passing initialization parameters to the constructor is omitted. As a result, all instances will obtain the same attribute value by default.

The biggest problem with the prototype pattern is caused by its shared nature. All properties in the prototype are shared by many instances

This kind of sharing is very suitable for functions. For properties containing reference types, the problem is more prominent.

④ Therefore, the prototype pattern is rarely used alone.

⑸Combined use of constructor pattern and prototype pattern

①Combined use of constructor pattern and prototype pattern is the most common way to create a custom type.

②Constructor pattern is used to define instance properties, while prototype pattern is used to define methods and shared properties.

③ As a result, each instance will have its own copy of the instance attributes, but at the same time it will share references to methods, saving memory to the maximum extent.

⑹Other modes

①Dynamic prototype mode: Only when the constructor is called for the first time, the method is assigned to the corresponding attribute of the prototype object. Other examples are handled in the same way as the constructor mode

② Parasitic constructor mode: only encapsulates the code that creates the object, and then returns the newly created object, still using the new operator to call

③ Safe constructor mode: no public properties, only private Variables and methods, as well as some get/set methods to handle private variables.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What are the three types of JavaScript objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn