0

I am frequently using the different methods available on "Object" in javascript (eg. Object.create(null), Object.hasOwnProperty(...) etc.)

However I do not totally understand what Object actually is. I had a look at it with Firebug that, when typing Object says:

function Object() { [native code] }

This makes sense since I can use it as a constructor to create a new Object: new Object()

But if Object is a function, then how can it have methods in the say time?

The way I understand this is that when invoking let's say Object.create(null), create is a function that gets applied to the Object function. Is that true?

Some clarification would be appreciated.

Mikou
  • 579
  • 1
  • 8
  • 17
  • Somewhat related: http://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object – Pete TNT May 25 '16 at 10:53
  • From Mozilla - "JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. This chapter describes how to use objects, properties, functions, and methods, and how to create your own objects." – Harry May 25 '16 at 10:54

1 Answers1

1

However I do not totally understand what Object actually is.

It is defined in the specification.

But if Object is a function, then how can it have methods in the say time?

In JavaScript all functions are objects. Objects can have properties. Properties have values. Functions can be values.

function myFunction () {
  return 1;
}

myFunction.myMethod = function myMethod() {
  return 2;
}

document.body.appendChild(document.createTextNode(myFunction()));
document.body.appendChild(document.createTextNode(myFunction.myMethod()));

The way I understand this is that when invoking let's say Object.create(null), create is a function that gets applied to the Object function. Is that true?

In the sense that inside the create function, the value of this will be Object: yes.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335