如何在javascript中调用包含句点的函数';她叫什么名字?
如果我有以下对象:如何在javascript中调用包含句点的函数';她叫什么名字?,javascript,Javascript,如果我有以下对象: var helloWorldFunctions = { 'hello.world': function () { return 'hello world'}, 'helloWorld' : function () { return 'hello world'} } 我可以通过以下操作调用对象中的第二个“helloWo
var helloWorldFunctions = {
'hello.world': function () { return 'hello world'},
'helloWorld' : function () { return 'hello world'}
}
我可以通过以下操作调用对象中的第二个“helloWorld”函数:
helloWorldFunctions.helloWorld()
如何调用第一个“hello.world”函数?当然,执行以下操作会导致类型错误:
helloWorldFunctions.hello.world()
正如Rajaprabhu在评论中所建议的,您可以使用:
helloWorldFunctions['hello.world']()
该函数没有“名称”。相反,表达式
helloworldfunctions[“hello.world”]
(注意这只是一个普通的键查找)将返回一个可以调用的函数对象。这种访问(使用括号,提供字符串作为键)与访问对象中的任何其他值没有什么不同。helloWorldFunctions['hello.world']()
可以使用括号表示法。您可以使用helloWorldFunctions['hello.world']()
但是为什么名称中甚至会有句点?@user2864740:从ES2015开始就有了,在Chrome的最新版本中试试这个:(Firefox仍然不支持函数的name
属性)。@t.J.Crowder非常漂亮。