It's been long time since i have written a blog post as i am busy with the project deliverable. So, here i am with one more issue which i faced today while writing some basic javascript code.
The error is
javascript: typeerror (intermediate value) is not a constructor
i came across this issue when i am returning some object from a function. Following is the similar javascript code that i have written
function buildContact() {
return new { name: "John", mobile: "123-456-7890", country: "US" };
}
this is the javascript function which was showing the mentioned error.
Cause:
I used new which can be only used with a Function as operand. In this case i am using object as a constructor which is actually not.
Fix:
Here we have two options to fix this error.
1. To return object directly as shown below
function buildContact() {
return { name: "John", mobile: "123-456-7890", country: "US" };
}
2. Create a constructor and use the constructor to create new object as shown below
function Contact(name, mobile, country) {
this.name = name;
this.mobile = mobile;
this.country = country;
}
function buildContact() {
return new Contact("John", "123-456-7890", "US");
}
in this case we are returning the new Contact() by using constructor.
In this way you can fix the error javascript: typeerror (intermediate value) is not a constructor.
Please do comment if you know better way to fix this error. Thanks in advance.
To know more common javascript errors visit: Javascript
No comments:
Post a Comment