4

According to the ECMA-262 a native object

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

A built-in object is defined as

object supplied by an ECMAScript implementation, independent of the host environment, that is present at the start of the execution of an ECMAScript program.

with a 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.

If a native object is fully defined by the ECMA-262 specification rather than the host environment and an ECMAScript implementation may specify and define new built-in objects, how is it that these new built-in object's can be native objects when they are not fully defined by the ECMA-262 specification?

What am I missing?

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

3 Answers3

6

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.

"Built-in" objects are required to inherit from 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

Examples of host objects

  • DOM objects, document and window
  • console
Community
  • 1
  • 1
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • Are you sure there are no built-in objects whom `Object.getPrototypeOf(obj)` doesn't contain `Object.prototype` ? – Raynos Nov 08 '11 at 16:02
  • Note that WeakMap isn't a proprietary extension, it's an implementation of a Harmony proposal. Also `ActiveXObject()` is a host object and not a native object. – gsnedders Nov 08 '11 at 16:07
  • @gsnedders: `ActiveXObject()` *is* a native object, but it returns a host object. Even in older versions of IE and JScript, `ActiveXObject.prototype` is present whereas it is not on *all other* host objects. Also, `WeakMap()` is an extension until Harmony becomes ES 6 :-p – Andy E Nov 08 '11 at 16:10
  • @Raynos: I can't say for sure, but I believe that it would be a violation of the spec. Built-in objects are required to be native and therefore following the semantics laid out by ECMA-262. – Andy E Nov 08 '11 at 16:12
  • @gsnedders can one argue it is proprietary because it may differ from the finalized ES6 specification semantics of `WeakMap` ? – Raynos Nov 08 '11 at 16:14
  • @AndyE Whether or not the object has a `prototype` property doesn't change whether it is a host object or a native object. Try using a custom impl of [[Construct]] matching that of native objects, for example --- that won't work, so its semantics aren't entirely described by ES. – gsnedders Nov 08 '11 at 16:25
  • 1
    @Raynos @AndyE `Object.getPrototypeOf(Object.prototype) === null`. The `Object` prototype object is the only built-in which doesn't have the `Object` prototype object in its prototype chain. – gsnedders Nov 08 '11 at 16:26
  • 1
    @Raynos It's not proprietary because it's sufficently specified to be implemented by anyone. It's not standard, true, but it's not proprietary. – gsnedders Nov 08 '11 at 16:27
  • @gsnedders: word *'proprietary'* retracted. My argument wasn't that `ActiveXObject` was a built-in object because it has a `prototype`, but rather that having a `prototype` in environments whose host objects don't have one is evidence that it's provided by the JavaScript implementation (JScript) and not the host environment. – Andy E Nov 08 '11 at 16:32
  • @AndyE I think that line between JScript and host environment is implementation specific. My understanding is that (ignoring the JIT) everything is translated from js source code into C++ objects. Some implement more javascript object like interfaces then others. having a split between JScript objects or host objects is strange. – Raynos Nov 08 '11 at 16:36
  • @AndyE I think, `window` is a native object, not a host object. I have created [this question](http://stackoverflow.com/q/10553188/425275). – Šime Vidas May 11 '12 at 14:39
2

The semantics of a native object are fully defined by ECMA-262. The object itself may not be.

So we have three levels of objects:

  1. Standard built-in objects: defined ECMA-262, and follow ECMA-262 semantics. Example: Object.
  2. Other built-in objects: not defined in ECMA-262, but follow ECMA-262 semantics. Example: setTimeout.
  3. Host objects: don't follow ECMA-262 semantics; they can have weird behavior of any sort, and interact with EMCA-262 built-in objects in weird and unexpected ways (for example lying about various internal properties). Example: NodeList.
Domenic
  • 110,262
  • 41
  • 219
  • 271
  • `setTimeout` isn't a built-in object, it's a host object (it's part of the DOM). – Andy E Nov 08 '11 at 15:40
  • I'm not entierly clear myself, but I am pretty sure it follows ECMA-262 semantics, which would make it a built-in by these definitions, right? – Domenic Nov 08 '11 at 16:13
  • host objects can follow ECMA-262 semantics if they want to. Sadly, in older versions of IE and other browsers, this isn't the case. Even `setTimeout instanceof Function` returns `false` and `typeof setTimeout` returns `"object"` in IE 7 and lower. – Andy E Nov 08 '11 at 16:17
0

The definition of native object is the relative term of host object
Like window.console object is a host object which Ecma262 haven't been documented to tell the browser how to implement it.
And Ecma262 is just a project to specify the language the feature. ECMAScript implementation actually is browser's business. That means the implementation compliant to the specification on the object type, we can say it built-in object though they does not work exactly the same.

Kelly Apollo
  • 350
  • 2
  • 14