+ 1

What am I doing wrong???

function person(name, age, job, car) { this.name=name; this.age=age; this.job=job; this.car=car; this.dob=function (age){ return(2016-this.age); } Dob thing is not working at all.... It prints the whole function...

28th Sep 2016, 4:29 PM
Chitrarth Sahai
Chitrarth Sahai - avatar
14 Answers
+ 3
Call the function
28th Sep 2016, 7:57 PM
Ashish
Ashish - avatar
+ 2
no need to pass age parameter inside dob method function person(name, age, job, car) { this.name=name; this.age=age; this.job=job; this.car=car; this.dob=function (){ return(2016-this.age); } } var p=new person(1,2,3,4); alert(p.dob())
29th Sep 2016, 6:20 PM
Moorthi R
Moorthi R - avatar
+ 1
Var person = new Person(1,2,3,4); Person.myFuntion()
29th Sep 2016, 2:08 AM
Cem Arguvanlı
Cem Arguvanlı - avatar
+ 1
you need to call function as below var p=new person(1,2,3,4) p.dob() // this will return the age
29th Sep 2016, 6:31 AM
sachin
0
I guess objectname.dob does that only
29th Sep 2016, 1:27 AM
Chitrarth Sahai
Chitrarth Sahai - avatar
0
@cem but the function returns the answer to the Dob property.... So person.dob should also work huh?? Should I use function (this.age){.........}
29th Sep 2016, 4:44 AM
Chitrarth Sahai
Chitrarth Sahai - avatar
0
@sachin it is now just printing NaN
29th Sep 2016, 10:16 AM
Chitrarth Sahai
Chitrarth Sahai - avatar
0
@chitrarth this giving me right answer function person(name, age, job, car) { this.name=name; this.age=age; this.job=job; this.car=car; this.dob=function (age){ return(2016-this.age); } } var p=new person(1,2,3,4); alert(p.dob())
29th Sep 2016, 11:20 AM
sachin
0
@sachin I have tried the same..... But it shows NaN.... But when I define the function after the object constructor it works fine.... Wtf right?? BTW iam using sublimetext
29th Sep 2016, 3:37 PM
Chitrarth Sahai
Chitrarth Sahai - avatar
0
read first idea
4th Oct 2016, 4:47 PM
AmirParsa Karimi
AmirParsa Karimi - avatar
0
yeah ok
9th Oct 2016, 8:50 PM
Nicola Lacerenza
Nicola Lacerenza - avatar
0
It works: function person(name,age){ this.name=name; this.age=age; this.yearOfBirth=yearBorn; } function yearBorn(){ return 2016-this.age; } var p1 = new person("Eugene",35); yearBorn(); document.write(p1.name," is ",p1.age," y.o.<br/> He was born in ",p1.yearOfBirth()," year.<br/>");
6th Nov 2016, 9:18 AM
Eugene Lavrinenko
Eugene  Lavrinenko - avatar
0
at least in your code here, you're missing a } at the end of your code.
16th Nov 2016, 2:09 PM
Mohamed Elliethy
Mohamed Elliethy - avatar
0
Call the function after the first argument
30th Dec 2016, 7:32 AM
JOHN MBONE KIRANGA
JOHN MBONE KIRANGA - avatar