Skip to content
Advertisement

how to combine first name and last name in javascript?

**"error"
"TypeError: Person.fname is not a function
    at Person.fullname (hovaqedile.js:14:17)
    at hovaqedile.js:19:40
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:13924
    at https://static.jsbin.com/js/prod/runner-4.1.4.min.js:1:10866**

hi

how to combine first name and last name in javascript ? I am getting error

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return Person.fname() + Person.lname()
}

var p = new Person();

console.log(p.fullname())

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.

function Person(){

}

Person.prototype.fname=function(){
  return "abc"
}

Person.prototype.lname=function(){
  return "lop"
}

Person.prototype.fullname=function(){
  return this.fname() + this.lname()
}

var p = new Person();

console.log(p.fullname())
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement