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.
-
Somewhat related: stackoverflow.com/questions/9108925/…Pete TNT– Pete TNT2016年05月25日 10:53:46 +00:00Commented May 25, 2016 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– Harry2016年05月25日 10:54:22 +00:00Commented May 25, 2016 at 10:54
1 Answer 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.