1

I have made an arrow function as below, nested in the getdata function, when I call the getdata function, it outputs undefined, what am I doing wrong?

let person = {
    name: "Mario",
    age: 30,
    salary: 5555,
    getdata: function () {

        welcome = () => {
            return `hello: ${this.name} your age is ${this.salary}`
        };
    },
}

console.log(person.getdata())

2
  • 1
    you are not return anything from that funtion so of course its going to give you undefined Commented Jun 29, 2020 at 22:44
  • You don't return anything directly in getdata, the only return is inside the arrow function expression set to welcome which isn't a return for getdata Commented Jun 29, 2020 at 22:44

6 Answers 6

2

I think you mean to do this.

let person = {
    name: "Mario",
    age: 30,
    salary: 5555,
    getdata:  () => {
            return `hello: ${this.name} your age is ${this.salary}`
        };
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer , but i'm practicing on the nested function , so i'm trying to display the sentence by this way
0
let person = {
    name: "Mario",
    age: 30,
    salary: 5555,
    getdata: function () {
        return {
        welcome: function() {
            return `hello: ${this.name} your age is ${this.salary}`

}
}
        
    },
}

this will return you the desired result:

person.getdata().welcome()

1 Comment

It looks to me like you want to return a function from a function. so you can use this approach. or you can directly return getdata() as shown in other answers
0

Since getData() is just a function that defines another function but doesn't return anything, the return value from calling it is undefined. But, if you remove the nested function, and just return the data you want, you will get a return value:

let person = {
    name: "Mario",
    age: 30,
    salary: 5555,
    getdata: function () {
        return  `hello: ${this.name} your age is ${this.salary}`
    },
}

console.log(person.getdata());

Comments

0

It's because getdata() isn't returning anything. It just has a nested function. If all you want is to return the contents of welcome, this is what you would put.

let person = {
    name: "Mario",
    age: 30,
    salary: 5555,
    getdata: function () {
          return `hello: ${this.name} your age is ${this.salary}`
    }
}

Comments

0

you can't passing context in a static Object, the best mode to solve your problem is by js class:

class person {
  constructor(data) {
    this.info = data;
  }

  welcome(){
    return `hello: ${this.info.name} your age is ${this.info.age}`
  }

  getSalary(){
    return this.info.salary;
  }

  getAge(){
    return this.info.age;
  }

}

var mario = {
    name: "Mario",
    age: 30,
    salary: 5555,

}


var data = new person(mario);

console.log(data.welcome());
console.log(data.getSalary());
console.log(data.getAge());

Comments

0

The problem is that your getdata function doesn't return anything. What you want is to create an object person that has a property getdata, which is a function that returns a nested object that also has a property welcome that is a function that returns a string.

I think what you're trying to achieve is the following:

let person = {
    name: "Mario",
    age: 30,
    salary: 5555,
    getdata: function () {
        return {
            welcome: () =>  `hello: ${this.name} your age is ${this.salary}`
        }
    }
}

So you can do:

console.log(person.getdata().welcome()) //"hello: Mario your age is 5555"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.