Home > Article > Web Front-end > What is the difference between host objects and native objects in javascript
Difference: Native objects are objects provided by the ECMAScript implementation independent of the host environment, that is, classes (reference types) defined by ECMA-262; while host objects are objects provided by the host environment implemented by ECMAScript, and all non- Native objects are host objects.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The difference between built-in (Build-in) objects and native (Naitve) objects is that the former is always an object created during the engine initialization phase and is a subset of the latter; while the latter includes some Objects created dynamically during runtime.
Engine extension objects are a not too large set. Generally speaking, they are relatively certain. They also belong to the native objects of the engine (but do not belong to the native objects of the ECMA specification).
The host object is not a native object of the engine, but an object registered to the JavaScript engine by the host framework through some mechanism.
Some hosts will also call the objects/constructors they provide "native objects". For example, Internet Explorer 7 calls the XMLHttpRequest() it provides native - in contrast, in it Objects created through methods such as "new ActiveXObject('Microsoft.XMLHTTP')" in earlier versions of . In this case, readers should note the difference between "host's native objects" and "engine's native objects".
1. Native objects
ECMA-262 defines native objects as “objects provided by ECMAScript implementations that are independent of the host environment.”
Object, Function, Array, String, Boolean, Number, Date, RegExp, Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, ActiveXObject (server), Enumerator (collection traversal class), RegExp (Regular expression)
It can be seen from this that, simply speaking, the native object is the class (reference type) defined by ECMA-262.
2. Built-in objects
ECMA-262 defines built-in objects as “all objects provided by the ECMAScript implementation that are independent of the host environment , appears when an ECMAScript program starts executing." This means that the developer does not have to explicitly instantiate the built-in object, it is already instantiated.
It is also "independent of the host environment". According to the definition, it seems difficult to distinguish the difference between "built-in objects" and "native objects". ECMA-262 only defines two built-in objects, namely Global and Math (they are also native objects, and by definition, every built-in object is a native object).
That’s understandable. Built-in objects are a type of native objects. Among the two objects it contains, we often use the Math object, but what is this Global object?
The Global object is the most special object in ECMAScript, because in fact it does not exist at all! In ECMAScript, there are no independent functions, all functions must be methods of an object.
Similar to the isNaN(), parseInt() and parseFloat() methods, they all appear to be functions, but in fact, they are all methods of the Global object. And the Global object's methods don't stop there.
3. Host object
What is a "host object"? The "host" in ECMAScript is of course the running environment of our web page, that is, the "operating system" and "browser". All non-native objects are host objects, that is, objects provided by the host environment implemented by ECMAScript.
All BOM and DOM objects are host objects. Because it displays different contents for different "host" environments. In fact, to put it bluntly, the objects that are not officially defined by ECMAScript belong to the host object, because most of the undefined objects are objects created by the ECMAScript program. TML DOM is a W3C standard (the abbreviation of HTML Document Object Model, Document Object Model for HTML).
HTML DOM defines a set of standard objects for HTML, as well as standard methods for accessing and processing HTML documents.
Through the DOM, you can access all HTML elements, along with the text and attributes they contain. The content can be modified and deleted, and new elements can also be created.
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What is the difference between host objects and native objects in javascript. For more information, please follow other related articles on the PHP Chinese website!