gpt4 book ai didi

javascript - 连接 Javascript 函数和字符串

转载 作者:行者123 更新时间:2023-11-28 17:47:53 24 4
gpt4 key购买 nike

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

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

感谢您的帮助!

最佳答案

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

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

关于javascript - 连接 Javascript 函数和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46286831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com