Jump to content

variable.constructor Javascript.

mrchow19910319
Go to solution Solved by 0x21,

Dog() is a call to the function without passing a parameter for the name.

Dog is the name of the function.

 

What you are doing in this case is:

console.log(new_variable.constructor, Dog(), new_variable.constructor === Dog());
// [Function: Dog] undefined false

You can read more about constructors here.

let’s say that there is a constructor function here:

function Dog(name){
	this.name = name;
}

let new_variable = new Dog("puppy");

console.log(new_variable.constructor);
console.log(new_variable.constructor===Dog);

the 1st console.log will return you a function , because .constructor will return you a reference of the constructor function, following this logic if .constructor will return you a function, shouldn’t be the second statement becomes:

 

new_variable.constructor===Dog()

??

 

 

542493407_ScreenShot2018-10-16at6_12_00PM.png.429aecd3bb04885aef4fbde7090e7167.png

 

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm really not sure what you're trying to achieve, but what I can tell for sure is that "===" is a comparison operator and will only return a Boolean value.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, IAmAndre said:

I'm really not sure what you're trying to achieve, but what I can tell for sure is that "===" is a comparison operator and will only return a Boolean value.

why it is if(newvariable.constructor===Dog) instead of if(newvariable.instructor===Dog())

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

Dog() is a call to the function without passing a parameter for the name.

Dog is the name of the function.

 

What you are doing in this case is:

console.log(new_variable.constructor, Dog(), new_variable.constructor === Dog());
// [Function: Dog] undefined false

You can read more about constructors here.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×