skaterdav85 Posted July 20, 2011 Share Posted July 20, 2011 Does anyone understand the hierarchy of ECMAScript Reference and Primitive types? I noticed that Object.constructor returns Function, yet I read that all the reference and primitive types inherit from the Object reference type. If Object.constructor returns Function, but Function inherits from Object, isn't that like saying the top most object Object is dependent on it's child Function? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 20, 2011 Share Posted July 20, 2011 Object is the base type. A function is a subclass of an object. A function is an object, but an object is not a function. The base object class does not inherit from a function, the base function class inherits from the base object. Link to comment Share on other sites More sharing options...
skaterdav85 Posted July 20, 2011 Author Share Posted July 20, 2011 "A function is a subclass of an object." Don't you mean, Function is a subclass of Object? A function would be an instance of Function."The base object class does not inherit from a function"Then why does Object.constructor equal Function? Link to comment Share on other sites More sharing options...
Ingolme Posted July 20, 2011 Share Posted July 20, 2011 "A function is a subclass of an object." Don't you mean, Function is a subclass of Object? A function would be an instance of Function."The base object class does not inherit from a function"Then why does Object.constructor equal Function?Because all constructors are functions. Constructor is the function that runs when an object is instantiated. Link to comment Share on other sites More sharing options...
skaterdav85 Posted July 20, 2011 Author Share Posted July 20, 2011 so if Object is a constructor, and all constructors are functions, and functions are objects of Function, wouldnt that mean Object is an instance of Function? Link to comment Share on other sites More sharing options...
Ingolme Posted July 20, 2011 Share Posted July 20, 2011 An object is not a constructor. An object has a constructor. An object is an instance of Object.Object is an object. Object.constructor is a function. The constructor, not the object itself. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 20, 2011 Share Posted July 20, 2011 Then why does Object.constructor equal Function?It doesn't equal function, it is a function. The constructor is a function that gets executed when a new object gets created. The constructor does not create the actual object, it is optional code that runs after the object gets created to run whatever initialization code your object instance needs. I think you're conflating the constructor with the object, but they're not the same. The constructor is optional, if present it gets executed after the object gets created. Link to comment Share on other sites More sharing options...
skaterdav85 Posted July 20, 2011 Author Share Posted July 20, 2011 ok so let me get this straight. there are reference and primitive types provided in ECMAScript. They are String, Number, Function, Boolean, and Object. Object is an object, Number is an object, etc, and their constructor property is a function (an instance of Function) that gets executed to run whatever initialization code the object instance needs. String, Number, Function, etc all inherit from Object. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 20, 2011 Share Posted July 20, 2011 That sounds right. Link to comment Share on other sites More sharing options...
skaterdav85 Posted July 20, 2011 Author Share Posted July 20, 2011 Actually, I take some of that back. The constructor property of String, Number, etc isn't a function that is an instance of Function. I think it is just the built in Function constructor function. function Person (name) { this.name = name;}var david = new Person ('david');david.constructor; // Person () function was used to initialize the david objectvar someObject = new Object ();someObject.constructor; // Object () function was used to initialize the someObject objectObject.constructor; //Function () function was used to initialize the Object object Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.