2

I've one deep confusion in javascript regarding constructors. Here you go -

objects in javascript are created from the constructors, right?

and constructors are created from the constructor function, right?

and functions in javascript are also objects but functions in javascript are also created from the Function constructor, right?

So to be purely specific is it okay to say constructors in javascript are objects or is it wrong to say like that?

So, in the end, what will you call these, to be pure specific, just constructors or objects?

 Math
 Number
 Array
 Object

I've seen on MDN documentation these are listed under pre-built objects. Does that conclude constructors are objects?

Also, it is mentioned everywhere that in javascript everything is an object.

So could anyone please clarify for me if is it okay to say constructors are also objects in javascript?

If it is yes then, HOW DOES A CONSTRUCTOR BECOMES OBJECT (and vice versa) IN JAVASCRIPT, PLEASE THROW SOME LIGHT?

eglease
  • 2,445
  • 11
  • 18
  • 28
Nishant Kumar
  • 691
  • 1
  • 11
  • 31
  • 4
    Yeah, everything's an object in JS, except for primitives (and even those can have `.prototype` methods called on them) – CertainPerformance Oct 14 '21 at 00:46
  • Yeah, but how does the constructor become an object, please explain this? because they say objects are created from constructors? – Nishant Kumar Oct 14 '21 at 00:48
  • 3
    It doesn't really "become" an object, it's always an object, like everything else you ever encounter (except for primitives). Kind of like asking "How does 5 become a number" - that's just how things are – CertainPerformance Oct 14 '21 at 00:48
  • 3
    A constructor is just an object with a [\[\[Construct\]\] internal method](//tc39.es/ecma262/#sec-object-internal-methods-and-internal-slots). – Sebastian Simon Oct 14 '21 at 00:49
  • @NishantKumar What is so strange about an object creating another object? Functions can create other functions: `() => () => {}`. – Sebastian Simon Oct 14 '21 at 00:50
  • There must have been some process of creating Math or Array with their prototype and static properties – Nishant Kumar Oct 14 '21 at 00:50
  • 4
    Note that `Math` isn’t a constructor, but it still is an object. Since `Math` and `Array` are native objects, the JS environment has to provide these; the process of how these are created is obscured. But you can create your own class with a prototype and static properties: `class X { static y = 42 }`, or a namespace-like object: `const X = { y: 42 };`. All constructors are objects, all functions are objects. Not all objects are constructors, not all objects are functions. Primitives are not objects and objects are not primitives. – Sebastian Simon Oct 14 '21 at 00:52
  • 1
    Math is not a constructor, why? it does starts from the capital first letter. why does it start from the capital first letter why not small if it is not a constructor? Do you mean pre-built objects have any special privilege to start from the capital first letter? I thought only constructors follow that convention. – Nishant Kumar Oct 14 '21 at 00:57
  • 2
    No, constructors and namespaces start with a capital letter. This is mentioned in the [doocumentation](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Math#description). It’s obviously not a constructor because `new Math` will throw a TypeError, literally stating “`Math` is not a constructor”. Being built-in is irrelevant. Note that “namespace” is an informal term in JS. See [In javascript, what is the difference between an object and a namespace?](/q/38366871/4642212) and [What is meant by 'JavaScript Namespacing'?](/q/8523231/4642212). – Sebastian Simon Oct 14 '21 at 00:58
  • the documentation also says it's not a function object. so, it's a kinda exception I guess, it might be the namespace. I believe rest must be function objects. What's a function object? objects created from the function constructor, right? please please answer. – Nishant Kumar Oct 14 '21 at 01:06
  • 2
    See [What is the difference between a function call and function reference?](/q/15886272/4642212) and [What is meant by 'first class object'?](/q/705173/4642212). “Constructors”, “constructor objects”, “functions”, and “function objects” are all a description for the same thing, generally. There is only a runtime difference between functions and constructors, e.g. `Map()` and `new Math.sin()` will not work; one is not callable, the other not constructable. – Sebastian Simon Oct 14 '21 at 01:11
  • All constructors are objects, Not all objects are constructors. So what makes an object (assuming prebuilt object constructor ) a constructor in javascript? – Nishant Kumar Oct 14 '21 at 02:23
  • @NishantKumar I already told you that. See my first comment. – Sebastian Simon Oct 14 '21 at 02:24
  • @SebastianSimon you mean this link https://tc39.es/ecma262/#sec-object-internal-methods-and-internal-slots unfortunately my system hangs and crashes while loading this website :( – Nishant Kumar Oct 14 '21 at 02:26
  • 1
    @NishantKumar Oh. Well, try [the multipage one](//tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-object-type) then. But it also doesn’t matter. It’s just an internal property that controls this behavior. – Sebastian Simon Oct 14 '21 at 02:30

0 Answers0