连接Javascript函数和字符串

时间:2017-09-18 19:30:13

标签: javascript

我试图将函数返回的结果连接到一个简单的字符串,两者都在同一个对象中声明。 例如:

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

以下是Fiddle

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

你可以使用构造函数来创建对象,如下所示:

>>> SeqF = []
>>> SeqF = range(0, 8)
>>> SeqF[0] = [1,0,0,0]
>>> SeqF[1] = [1,1,0,0]
>>> SeqF[2] = [0,1,0,0]
>>> SeqF[3] = [0,1,1,0]
>>> SeqF[4] = [0,0,1,0]
>>> SeqF[5] = [0,0,1,1]
>>> SeqF[6] = [0,0,0,1]
>>> SeqF[7] = [1,0,0,1]
>>> SeqF
[[1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 1]]

答案 1 :(得分:0)

这应该有效:

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