简体   繁体   中英

Does JavaScript have exotic objects?

I want to write a series of JavaScript related articles/tutorials. I was looking up the ECMA specification when I discovered this interesting paragraph.

As ECMA-262 (Version 6) states:

4.3.7 exotic object

object that does not have the default behaviour for one or more of the essential internal methods that must be supported by all objects

NOTE Any object that is not an ordinary object is an exotic object.

Now I am curious. Are such exotic objects to be found in modern browser's JavaScript?

If so: Could one give me an example and tell in how far its behaviour is different from 'ordinary objects'?

A possible example:

The instance objects created by Array are exotic (a term used by the ECMAScript specification for objects that have features that normal objects don't have): Their property length tracks and influences the management of array elements. In general, exotic objects can be created from scratch, but you can't convert an existing normal object into an exotic one.

Taken from http://www.2ality.com/2015/02/es6-classes-final.html

There's also a list later in the spec including Array, String, and so on.

They're not "exotic" in the sense of mysterious and/or sexy.

Section 9.4 of the same document lists exotic objects. For example an array is such an object. Among other things, it's [[DefineOwnProperty]] internal method is different from the standard method defined for objects in that it treats numerical "keys" differently.

DOM arrays vs Array

if you try in a browser document.getElementsByTagName('*') the result looks to be an Array object containing all the DOM elements matching your query. But on that Array you cannot call most Array functionality as it's not really an Array.

here is a example :

 var normalArray = [{objectId: 1}, {objectId: 2}, {objectId: 3}, {objectId: 4}]; var domArray = document.getElementsByTagName('*'); log(normalArray.length + ' objects in normalArray'); log(domArray.length + ' objects in domArray'); printArray(normalArray); printArray(domArray); function printArray(array){ // lets try with array try{ log('printing normalArray'); array.forEach(function(data, index){ log('Iteration on ' + index + ' ok', 'success'); }); }catch(e){ log('failed because of ' + e, 'error'); } } function log(msg, className){ var logEl = document.createElement('pre'); logEl.innerText = msg; logEl.className = className || ''; document.getElementById('logs').appendChild(logEl); } 
 pre{ background: #eee; padding: 3px; margin: 1px; border-radius: 3px; } pre.error{ background: #fdd; } pre.success{ background: #dfd; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>some test</title> </head> <body> <h1>title</h1> <p> Some texts with some <a href="#">links</a> </p> <div id="logs"></div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM