Mikou
Mikou

Reputation: 651

What is javascript "Object"?

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.

Upvotes: 0

Views: 55

Answers (1)

Quentin
Quentin

Reputation: 943142

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.

Upvotes: 1

Related Questions