What does constructor==Object do?

To check whether something is an object I found out that we use this:

typeof instance === “object”

So what exactly does constructor==Object do or what are we doing it instead of doing typeof instance === “object”?

Primitive values like integers and strings don’t take a lot of memory so when you say:

let a = 5;
let b = a;

They both get their own copy of the number 5 so when you check for strict equality between the two: a === b what you are checking is if they are the same value which they are.

Objects however take up an arbitrary amount of memory so when you say:

a = {key: 'value'}
b = a

a an b reference the same object in memory and that object has its own unique id and that’s what gets checked when you do: a === b.

let a = 5;
let b = a;
a === b //true;

let c = {key: 'value'}
let d = c;
c === d //true
let e = {key: 'value'};
e === c //false

typeof returns a string of the type you use it on so

typeof {} === 'object' //true

instanceof checks if a constructors prototype property is in the objects prototype chain:

const o = {};
o instanceof Object //true

This true because o has The Object constructors prototype property in its prototype chain:

o = {}

o.__proto__
{constructor: ƒ, __defineGetter__: ƒ, 
constructor: ƒ Object()
arguments: (...)
assign: ƒ assign()
caller: (...)
create: ƒ create()
defineProperties: ƒ defineProperties()
defineProperty: ƒ defineProperty()
entries: ƒ entries()
freeze: ƒ freeze()
fromEntries: ƒ fromEntries()
getOwnPropertyDescriptor: ƒ getOwnPropertyDescriptor()
getOwnPropertyDescriptors: ƒ getOwnPropertyDescriptors()
getOwnPropertyNames: ƒ getOwnPropertyNames()
getOwnPropertySymbols: ƒ getOwnPropertySymbols()
getPrototypeOf: ƒ getPrototypeOf()
is: ƒ is()
isExtensible: ƒ isExtensible()
isFrozen: ƒ isFrozen()
isSealed: ƒ isSealed()
keys: ƒ keys()
length: 1
name: "Object"
preventExtensions: ƒ preventExtensions()
prototype: {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}

To answer “constructor==Object” I assume by constructor you are referring to a function the builds an object and in that case a function is essentially a special type of object and Object is itself a constructor so Object === Object would be true.

1 Like

Hi @ankurchaulagain ,

I’m trying to second-guess your question, but here are some points that might be of help to you:

  1. The typeof operator returns a string indicating the type of the unevaluated operand; this returns a string that indicates the type of operand being evaluated.

  2. Think of the relationship between an instance and its constructor as a “father-child” relationship; the instance being the child and the constructor being the father who created the child; just like in the real world, not all (children) ‘instances’ have the same (father) ‘constructor’, and vice versa.

  3. To create a constructor and an instance, you may do the following, as an example:

function Animal() {}

let dog = new Animal();
  1. For now, to verify if an instance (child) ‘is a child of’ constructor (father), you may use, as follows:
console.log(dog instanceof Animal); //will return true

console.log(dog.constructor == Animal); //will return true
  1. If you create an object, which is not an instanceof of a constructor, the above verifiers will return false, as follows:
function Animal() {}
let cow = {
              name: "Milky",
             numLegs: 4
};
console.log(cow instanceof Animal); //will return false
console.log(cow.constructor == Animal); //will return false

Note, as you move on with the lessons, things would become clearer for you. Hope this helps you somehow.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.