1

For example; when you create an array in JavaScript, all Objects prototype properties exists on it:

console.log(typeof [].valueOf); // function

I have read on several places that "almost everything is an object". I know the exceptions with primitive datatypes and how that works.

But what is actually meant by the quote? Is it that all datatypes prototype extends object.prototype? Or that they get an object wrapper?

I know that there are some similar threads, but I wouldn't say that they answer this.

Johan
  • 31,613
  • 48
  • 166
  • 272

4 Answers4

1

It means that "everything" that is an object has a prototype/prototype chain that eventually resolves down to 'Object'.

Watch/play with http://www.objectplayground.com/ for a while and you'll get it. It is an excellent resource for these kinds of conversations.

arb
  • 7,104
  • 6
  • 27
  • 62
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – John Dvorak Jan 19 '14 at 18:30
  • True. I just assumed that someone would have a better chance of articulating the answer here and wanted to provide this link because I found it extremely useful. – arb Jan 19 '14 at 18:50
  • If you don't want to quote the important parts here, you should post the link as a comment. Link-only answers are not welcome here. – John Dvorak Jan 19 '14 at 19:04
  • That link answers the question better than any text could ever hope to. – arb Jan 19 '14 at 19:10
1

You can test this for yourself (using the Node REPL, but will work in FF, Chrome, IE, etc)

> Object.prototype.test()
TypeError: Object #<Object> has no method 'test'
    at repl:1:19
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> Array.prototype.test()
TypeError: Object  has no method 'test'
    at repl:1:18
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> Object.prototype.test = function(){console.log('test');}
[Function]
> Array.prototype.test()
test
undefined
>
> Object.prototype.test()
test
undefined
> String.prototype.test()
test
undefined

If the Array prototype didn't include the Object prototype, this wouldn't work -- everything that is an object in JavaScript eventually has the Object.prototype as it's "parent".

tkone
  • 19,271
  • 5
  • 50
  • 77
1

Don't be confused by the shorthand syntax of []. Arrays can also be created using new Array() (not recommended), which may seem like a more familiar syntax (You may recognize it from using function constructors). When you investigate the prototype chain of the array using Object.getPrototypeOf() you always arrive at Object. Which proves the point your readings were trying to make Almost everything is an object.

var arr = new Array();
var constructor = Object.getPrototypeOf(Object.getPrototypeOf(arr)).constructor;
alert(constructor);

//prints: function Object() {
//          [native code]
//        }

alert(constructor === Object.prototype.constructor);
//prints true

JS Fiddle: http://jsfiddle.net/ay56U/2/

Kevin Bowersox
  • 88,138
  • 17
  • 142
  • 176
  • Only one should never use `new Array()` when creating an array in JavaScript. The preferred syntax is the Array literal (just like when creating an object -- never do `new Object()`. References: [1](https://coderwall.com/p/h4xm0w), [stackoverflow answer](http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-ar/1273936#1273936) – tkone Jan 19 '14 at 18:37
  • 1
    The __proto__ property is deprecated and should not be used. – Sterling Archer Jan 19 '14 at 18:37
1

There are no "datatypes".

Everything is an object.

It means that everything - function, array, instance, host api, data structure, whatever - is an object: A reference value with mutable properties.

Is it that all datatypes prototype extends object.prototype?

That not what is meant by the quote, but it is true that most of the standard objects inherit properties (via the prototype chain) from Object.prototype.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164