What is the difference between native objects and host objects?
Both terms are defined in the ECMAScript specification:
Source: http://es5.github.com/#x4.3.6native object
object in an ECMAScript implementation whose semantics are fully
defined by this specification rather than by the host environment.NOTE Standard native objects are defined in this specification. Some
native objects are built-in; others may be constructed during the
course of execution of an ECMAScript program.
Source: http://es5.github.com/#x4.3.8host object
object supplied by the host environment to complete the
execution environment of ECMAScript.NOTE Any object that is not native is a host object.
A few examples:
Native objects: Object
(constructor), Date
, Math
, parseInt
, eval
, string methods like indexOf
and replace
, array methods, ...
Host objects (assuming browser environment): window
, document
, location
, history
, XMLHttpRequest
, setTimeout
, getElementsByTagName
, querySelectorAll
, ...
`new Object` vs `Object` in the ECMAScript spec
Object(window)
will never clone window
but new Object(window)
might. All current -- potentially all known -- implementations just return the same reference, although the spec allows for implementation-defined behavior.
The steps for 15.2.1.1 say:
The definition of
- If value is null, undefined or not supplied, create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments
- Return ToObject(value).
ToObject
(9.9) lists a few types that will be caught by step 1 (in table 14), but for Object
has a very simple definition:The result is the input argument (no conversion).It explicitly states that the input argument will be returned as-is, so they should be equal references (
===
).The definition for new Object
(15.2.2.1) has a similar chain of type-checks in step 1, but the step for objects (1.a) is:
i. If the value is a native ECMAScript object, do not create a new object but simply return value.That is, for any host objectii. If the value is a host object, then actions are taken and a result is returned in an implementation-dependent manner that may depend on the host object.
foo
, the call Object(foo)
must === foo
but new Object(foo)
may === foo
.Host objects are defined in 4.3.8 to be
object supplied by the host environment to complete the execution environment of ECMAScript.This answer lists a few host objects to include
window
, history
, etc. Running those through new Object(foo)
should (but doesn't have to) return a different object.In any case but passing a host object, new Object(foo)
seems to be a more complicated chain that defers to ToObject
in much the same way as Object(foo)
.
Unfortunately, 15.2.2.1.1.a.ii states that the "result is returned in an implementation-dependent manner" and has no specifics as to the "actions [that] are taken" and it appears that Chrome will return the same object (equal references) for all of the listed "host objects."
Using this script to check:
var objects = [
/* Native objects */
'Object', 'Date', 'Math', 'parseInt', 'eval',
/* Host objects */
'window', 'document', 'location', 'history', 'XMLHttpRequest', 'setTimeout'
];
function getDefinedReference(name) {
if (eval('typeof ' + name) !== 'undefined') {
return eval(name);
} else {
throw new Error('' + name + ' is not defined.');
}
}
function checkIdentity(name) {
try {
var ref = getDefinedReference(name);
var no = new Object(ref);
var o = Object(ref);
console.log(name, ref === no, ref === o, no === o);
if (ref === o && no !== o) {
// Make sure ref === Object(ref) but not new Object(ref)
console.log(name, 'returns different references.');
}
} catch (e) {
console.warn(e);
}
}
objects.forEach(checkIdentity);
if (typeof window !== 'undefined') {
for (var f in window) {
checkIdentity(f);
}
}
In ECMAScript, how are some of native objects also built-in?
Here is what ES5 shows:
As you can see, it's different that what you've shown.4.3.6
native object # Ⓣ
object in an ECMAScript implementation whose semantics are fully defined by this specification rather than by the host environment.NOTE Standard native objects are defined in this specification. Some native objects are built-in; others may be constructed during the course of execution of an ECMAScript program.
4.3.7
built-in object # Ⓣ
object supplied by an ECMAScript implementation, independent of the host environment, that is present at the start of the execution of an ECMAScript program.NOTE Standard built-in objects are defined in this specification, and an ECMAScript implementation may specify and define others. Every built-in object is a native object. A built-in constructor is a built-in object that is also a constructor.
Built-in objects are native objects made available by the ECMAScript-compliant engine. For example:
- String
- Object
- Array
- Undefined
- Boolean
- etc.
var obj = {};
Or the list shown before. Built-in objects are native.Also, you didn't show it, but a host object is an object dependant on the environment. For example, in browsers, the host object is window
. There are other host objects such as document
or XMLHttpRequest
though.
What is the difference between a 'standard object' and an 'ordinary object' in ECMAScript?
const user = { firstname: "Jonas" };
That's an ordinary object (as it is not exotic), however it is not a standard object, as the semantics are defined by me and not by the specification. Its behavior, however, is specified (e.g. user.firstname
will evaluate to "Jonas"
). What is an ECMAScript native object?
They're "native" because they come with the ECMAScript implementation. A host environment in generally an application consisting of an ECMAScript implementation and several other interfaces that work together. For instance,
- Web Browser — a host environment consisting of ECMAScript implementation, DOM interface, Rendering engine, UI, etc.
- Windows Script Host — a host environment consisting of ECMAScript implementation, VBScript implementation, etc.
- Node.js — a host environment consisting of ECMAScript implementation (V8), HTTP interfaces, etc.
Object
or Function
, whereas host objects — objects provided by the host environment, but not necessarily present at the start of execution — are not required to but may (and sometimes do).Examples of native objects defined by ECMA-262
Object()
,Array()
,Date()
Math
,JSON
, the Global object.
Examples of native, built-in objects not defined by ECMA-262
- Mozilla's numerous JavaScript extensions — such as
WeakMap()
or__proto__
- JScript's
ActiveXObject()
constructor and (undocumented)CollectGarbage()
function
Examples of host objects
- DOM objects,
document
andwindow
console
Related Topics
Check If File Exists But Prevent 404 Error in Console from Showing Up
Why Do Empty JavaScript Arrays Evaluate to True in Conditional Structures
JavaScript .Replace Only Replaces First Match
Passing a PHP Variable to JavaScript in a Blade Template
How to Bind to the Change Event of a Textarea in Jquery
Options for Testing Service Workers via Http
Converting a Buffer into a Readablestream in Node.Js
How to Convert Numbers Between Different Bases in JavaScript
JavaScript Property with Three Dots (...)
Why Does a Simple .*? Non-Greedy Regex Greedily Include Additional Characters Before a Match
How to Create a Date Object from String in JavaScript
Typeerror: Cannot Read Property 'Setstate' of Undefined
Can't Require() Default Export Value in Babel 6.X
Serialize Object to Query String in JavaScript/Jquery
D3.JSON Method Doesn't Return My Data Array