0

I am trying to concatenate the result returned by a function to a simple string, both are declared inside the same object. Example:

var hello = {
  how: function(){
    return ' are you';
  },
    ans: 'how',
    answer: 'how' + this.how() 
};

console.log(hello.how()); //works
console.log(hello.ans); //works
console.log(hello.answer); //doesnt work

Here is the Fiddle

Thanks for your help!

Crash Override
  • 411
  • 6
  • 17

2 Answers2

3

you can use a constructor function to create the object, something like this:

var hello = new function() {
  this.how = function(){
     return ' are you';
  },
  this.ans = 'how',
  this.answer = 'how' + this.how() 
};

console.log(hello.how()); //works
console.log(hello.ans); //works
console.log(hello.answer); //doesnt work
Dij
  • 9,761
  • 4
  • 18
  • 35
0

This should work:

var hello = {
  how: function(){
    return ' are you';
  },
    ans: 'how',
    answer: function(){
      return 'how' + this.how()
    }
};

console.log(hello.how()); //works
console.log(hello.ans); //works
console.log(hello.answer()); //now works
Nil
  • 401
  • 1
  • 6
  • 18