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.
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
Description | |
---|---|
Returns the absolute value of x. | |
Returns the arc cosine of x. | |
Returns the arcsine of x. | |
Returns the arctangent of x as a number between -PI/2 and PI/2 radians. | |
Returns the angle from the x-axis to the point (x,y) (between -PI/2 and PI/2 radians between). | |
Round the number up. | |
Returns the cosine of the number. | |
Returns the exponent of Ex. | |
Round x down. | |
Returns the natural logarithm of the number (base is e). | |
Returns the highest value among x,y,z,...,n. | |
Returns the lowest value among x,y,z,...,n. | |
Returns x raised to the y power. | |
Returns a random number between 0 ~ 1. | |
Rounding. | |
Returns the sine of the number. | |
Returns the square root of the number. | |
Returns the tangent of the angle. |
JavaScript Global Properties
Description | |
---|---|
represents a positive infinity value. | |
Indicates whether a value is a numeric value. | |
Indicates an undefined value. |
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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
