+ 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...
14 Answers
+ 3
Call the function
+ 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())
+ 1
Var person = new Person(1,2,3,4);
Person.myFuntion()
+ 1
you need to call function as below
var p=new person(1,2,3,4)
p.dob() // this will return the age
0
I guess objectname.dob does that only
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){.........}
0
@sachin it is now just printing NaN
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())
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
0
read first idea
0
yeah ok
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/>");
0
at least in your code here, you're missing a } at the end of your code.
0
Call the function after the first argument