【JavaScript】——"Re-Learning Front End" 04 Objects——Classification of Objects

Object Classification in JavaScript:

Host Objects : Objects provided by the JavaScript host environment whose behavior is entirely determined by the host environment.
Built-in Objects : Objects provided by the JavaScript language.
Intrinsic Objects (Intrinsic Objects) : Specified by the standard, object instances are automatically created along with JavaScript runtime creation.
Native Objects : Objects that can be created by users through built-in constructors such as Array and RegExp or special syntax.
Ordinary Objects : Objects created by the {} syntax, the Object constructor, or the class keyword definition class, which can be inherited by the prototype.

host object:

In the browser environment , we all know that the global object is window , and there are many attributes on window, such as document.

The properties on the global object window partly come from the JavaScript language and partly from the browser environment .


Host objects are also divided into inherent and user-creatable . For example, document.createElement can create some dom objects.
The host will also provide some constructors, for example, we can use new Image to create img elements.

Built-in objects · Intrinsic objects:

Intrinsic objects are object instances that are automatically created as the JavaScript runtime creates them, as specified by the standard.
Intrinsic objects are created before any JS code is executed, and they usually act like a base library. The "class" we mentioned earlier is actually a kind of inherent object.

Built-in objects and native objects:

In JavaScript, objects that can be created through the constructor of the language itself are called native objects.

According to different application scenarios, native objects are divided into the following categories.

 Through these constructors, we can use the new operation to create new objects, so we call these objects native objects.

Almost all of these constructor capabilities cannot be implemented with pure JavaScript code, nor can they be inherited with the class/extend syntax. Therefore, we can think that all these native objects are "privileged objects" designed for specific capabilities or performance.

Use objects to simulate functions and constructors:

Any object only needs to implement [[call]] , it is a function object and can be called as a function.

And if it can implement [[construct]] , it is a constructor object and can be called as a constructor.

A function created by the user with the function keyword must be both a function and a constructor . However, their
behavioral effects are not the same .

For host and built-in objects, their implementation of [[call]] (called as a function) and [[construct]] (called as a constructor
) are not always consistent.

//内置对象 Date 在作为构造器调用时产生新的对象,作为函数时,则产生字符串
 console.log(new Date); // 1
 console.log(Date()); //Tue Dec 06 2022 08:47:16 GMT+0800 (中国标准时间)
//浏览器宿主环境中,提供的 Image 构造器,则根本不允许被作为函数调用。
console.log(new Image); // <img>
console.log(Image());// 抛出错误

Another example is the basic types (String, Number, Boolean), and their constructors are called as functions, which will produce the effect of type conversion.


For objects created by the user using the function syntax or the Function constructor , [[call]] and [[construct]] always behave similarly, and they execute the same piece of code.

function f(){
 return 1;
}
var v = f(); // 把 f 作为函数调用
var o = new f(); // 把 f 作为构造器调用

Among intrinsic objects and native objects, some objects behave very differently from normal objects:

Their common subscript operations (that is, using square brackets or dots for attribute access) or setting prototypes are different from ordinary objects:

Array: The length property of Array automatically changes according to the largest subscript.
Object.prototype: As the default prototype of all normal objects, it can no longer be set to a prototype.
String: In order to support subscript operation, the positive integer attribute access of String will search in the string.
Arguments: The non-negative integer subscript attribute of arguments is linked with the corresponding variable.
The namespace object of the module: there are many special places, completely different from the general objects, try to only use it for import.
Type array and array buffer: associated with the memory block, the subscript operation is special.
function after bind: associated with the original function.

Guess you like

Origin blog.csdn.net/qq_50497708/article/details/128181580