JavaScript
x
7
1
**"error"
2
"TypeError: Person.fname is not a function
3
at Person.fullname (hovaqedile.js:14:17)
4
at hovaqedile.js:19:40
5
at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13924
6
at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866**
7
hi
how to combine first name and last name in javascript ? I am getting error
JavaScript
1
20
20
1
function Person(){
2
3
}
4
5
Person.prototype.fname=function(){
6
return "abc"
7
}
8
9
Person.prototype.lname=function(){
10
return "lop"
11
}
12
13
Person.prototype.fullname=function(){
14
return Person.fname() + Person.lname()
15
}
16
17
var p = new Person();
18
19
console.log(p.fullname())
20
can you please tell how I will print call first name and lastname in javascript
Advertisement
Answer
You should use this
keyword which is a reference to the current object.
JavaScript
1
19
19
1
function Person(){
2
3
}
4
5
Person.prototype.fname=function(){
6
return "abc"
7
}
8
9
Person.prototype.lname=function(){
10
return "lop"
11
}
12
13
Person.prototype.fullname=function(){
14
return this.fname() + this.lname()
15
}
16
17
var p = new Person();
18
19
console.log(p.fullname())