+ 2
Why does this output undefined? And how can I solve it?
12 Answers
+ 1
Done thanks
+ 1
In function, return this pointer.
And then you use like this without new
function Person(firstName,lastName,age,eyeColor) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.eyeColor = eyeColor;
return this; //return function pointer
}
const f = Person(firstName , lastName , age);
+ 1
Yes. It works but create second object in same as first object.
You are creating second object with previous first inputs, next changing through inputs. So father.age is same as mothers.age, instead of undefined because you given any value still.
If not problem then, leave as it is..
+ 1
Thanks for your help
0
//you are not setting Function values. And also you can't use out side function that way..
function Person(firstName,lastName,age,eyeColor) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.eyeColor = eyeColor;
}
firstName =prompt("write first name");
lastName = prompt("Write last name");
age = prompt("Write your age");
var f = new Person(firstName ,lastName ,age);
document.write("first name is " + f.firstName + " " +"and she is" + f.age + " years old");
0
Jayakrishna🇮🇳 , I still get undefined, it didn't work.
0
It seems I can only use predefined values with the Constructor
0
How do I write a function that accept input and output it back?
0
Observe my reply carefully..
Try that, just copy and paste in your code. Run it and see..
Your object creation is still wrong.
0
What if I don't want to use "new" ? What can I use to replace it?
0
Jayakrishna🇮🇳 can you check my code again, did I do everything right this time?
0
You're welcome..