Khác nhau giữa host objects và native objects là gì?

I don"t understand the difference between native objectshost objects in JavaScript. Does the latter simply refer to lớn non-primitive sầu function objects that were created by a custom constructor (e.g., var bird1 = new Bird();)?



A DOM element -- say, new Image() -- is a host object, for instance. –web1_user166390 Sep 30 "11 at 18:09

Both terms are defined in the ECMAScript specification:

native object

object in an ECMAScript implementation whose semantics are fullydefined by this specification rather than by the host environment.

Bạn đang xem: Khác nhau giữa host objects và native objects là gì?

NOTE Standard native objects are defined in this specification. Somenative objects are built-in; others may be constructed during thecourse of execution of an ECMAScript program.


host object

object supplied by the host environment to complete theexecution environment of ECMAScript.

NOTE Any object that is not native sầu is a host object.


A few examples:

Native sầu objects: Object (constructor), Date, Math, parseInt, eval, string methods like indexOf and replace, array methods, ...

Xem thêm: Cách Tìm Kiếm File Trong Win 7, Cách Tìm Kiếm File Trong Windows 7

Host objects (assuming browser environment): window, document, location, history, XMLHttpRequest, setTimeout, getElementsByTagName, querySelectorAll, ...

Improve sầu this answer
edited Jun trăng tròn "đôi mươi at 9:12

111 silver badge
answered Sep 30 "11 at 18:13

Šime VidasŠime Vidas
164k5959 gold badges261261 silver badges366366 bronze badges
| Show 18 more comments
It is more clear if we distinguish between three kinds of objects:

Built-in objects: String, Math, RegExp, Object, Function etc. - core predefined objects always available in JavaScript. Defined in the ECMAScript spec.

Host objects: objects like window, XmlHttpRequest, DOM nodes and so on, which is provided by the browser environment. They are distinct from the built-in objects because not all environment will have the same host objects. If JavaScript runs outside of the browser, for example as server side scripting language like in Node.js, different host objects will be available.

User objects: objects defined in JavaScript code. So "Bird" in your example would be a web1_user object.

The JavaScript spec groups built-in objects & web1_user objects together as native objects. This is an unorthodox use of the term "native", since web1_user objects are obviously implemented in JavaScript while the built-ins is most likely implemented in a different language under the hood, just as the host objects would be. But from the perspective of the JavaScript spec, both builtins và web1_user objects are native to lớn JavaScript because they are defined in the JavaScript spec, while host objects are not.