It is my understanding that pretty much everything in JS is an object and that constructors are one way to make a new object. I know that data types like Array, Function, and Objects all have to use the new keyword, thereby making them objects and on the backend works like new Array()
, new Function()
or new Object()
Please correct me if I am wrong. This is where things got confusing for me. I assigned x to an array [1, 2, 3, 4]
. When I inspected x, it showed that the [[prototype]]
is an Array and the constructor is the Array()
object. At the end, it showed the Array()
object's prototype which is Object and the constructor for the Object to be Object()
. I understood everything until I typed in Object.constructor
and the result was Function()
. Shouldn't it be Object()
, instead of Function()
? If the constructor of an object is new Object()
, why is it telling me that Function is the constructor? That way if I create a new object it will be instantiated like this: let x = new Function()
, yes? That was not the only one. Every primitive and reference data type gave the same result. Here is the code. I was so happy when I thought I grasped this knowledge and then I somehow I broke it. Hmmm, much like coding....lol. Thanks in advance for explaining.
let x = [1, 2, 3, 4];
undefined
x;
(4) [1, 2, 3, 4]
0: 1
1: 2
2: 3
3: 4
length: 4
**[[Prototype]]: Array(0)**
at: ƒ at()
concat: ƒ concat()
**constructor: ƒ Array()**
copyWithin: ƒ copyWithin()
entries: ƒ entries()
every: ƒ every()
...Many other Array methods...
**[[Prototype]]: Object**
**constructor: ƒ Object()**
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
__proto__: (...)
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
**Array.constructor;**
*ƒ Function() { [native code] }*
**Object.constructor;**
*ƒ Function() { [native code] }*
**Function.constructor;**
*ƒ Function() { [native code] }*
**String.constructor;**
*ƒ Function() { [native code] }*
**Boolean.constructor;**
*ƒ Function() { [native code] }*
**Number.constructor;**
*ƒ Function() { [native code] }*
CodePudding user response:
Object
is a function used to construct objects. Since it is a function, its own constructor is Function
.
CodePudding user response:
I think you could think this way, constructor
is a function that return an instance of object.
Now why you add new
is that so you create a new instance of the object.
Now class and function in js is almost the same.
Look below example
object example
class test {
constructor(name){
this.name = name;
}
getName(){
return this.name;
}
}
console.log(new test("hej").getName())
Function Example
function test(name){
this.name = name;
}
test.prototype.getName =function(){
return this.name;
}
console.log(new test("hej").getName())