在函数内构建哈希
我有一个函数,在给定某些参数的情况下返回哈希值:
build_message = function(from, to, bcc, subject, tag, htmlbody, textbody, replyto) {
message = {"From": from,
"To": to,
"Subject": subject,
"HtmlBody": htmlbody,
"TextBody": textbody};
message.encode();
}
其中一些参数(例如 bcc
和 replyto
)是可选的。如果调用者为它们提供空值,它们不得出现在我返回的哈希中。也就是说,只有当 bcc
参数非空时,"Bcc": bcc
才必须出现在哈希中。
这是我的第一次尝试,但解析器不喜欢它(这恰好在函数的 message.encode()
行之前):
bcc_body = bcc => {"Bcc": bcc} | {};
message.put(bcc_body);
正在使用 put()
允许对函数内的变量进行这样的操作吗?如果是这样,我的语法有问题吗?
I have a function that returns a hash, given certain parameters:
build_message = function(from, to, bcc, subject, tag, htmlbody, textbody, replyto) {
message = {"From": from,
"To": to,
"Subject": subject,
"HtmlBody": htmlbody,
"TextBody": textbody};
message.encode();
}
Some of these parameters, like bcc
and replyto
are optional. If the caller provides null values for them, they must not be present in the hash I return. That is to say, "Bcc": bcc
must only be present in the hash if the bcc
argument is non-null.
Here is my first attempt, but the parser doesn't like it (this goes right before the message.encode()
line of the function):
bcc_body = bcc => {"Bcc": bcc} | {};
message.put(bcc_body);
Is using the put()
operation allowed on a variable inside a function like this? If so, is something wrong with my syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在函数内使用
.put()
,但请记住,它返回一个新的哈希值并保持原始哈希值不变。尝试像这样结束你的方法:
You can use
.put()
inside a function, but remember that it returns a new hash and leaves the original unmodified.Try ending your method like this: