javascript - 如何在字典中打印以及 JS 中的这种结构是什么?
问题描述
我想知道为什么我们在 JS 中需要这个结构(?)以及我们如何在字典中打印 hello world
const fun = () => {
return { // Q: Is this called dictionary? How can I access to this?
matter: panging => { // Q: What's the purpose of having function here? how do we call it?
'the most'; // what's doing here?
console.log('hello world'); //How can I print this?
}
};
};
我们如何在这个函数中打印 hello world fun
,?
解决方案
Javascript 中没有字典。你所拥有的是一个具有matter
属性的对象。用 调用它fun().matter()
。fun
将返回一个对象,然后您可以访问(并调用)该matter
属性:
const fun = () => {
return { // Q: Is this called dictionary? How can I access to this?
matter: panging => { // Q: What's the purpose of having function here? how do we call it?
'the most'; // what's doing here?
console.log('hello world'); //How can I print this?
}
};
};
fun().matter();
这'the most'
条线什么都不做。您可以将其替换为任何其他表达式(没有副作用),您不会看到任何区别。
另一种看待它的方式:
const fun = () => {
return { // Q: Is this called dictionary? How can I access to this?
matter: panging => { // Q: What's the purpose of having function here? how do we call it?
'the most'; // what's doing here?
console.log('hello world'); //How can I print this?
}
};
};
const returnedObj = fun();
returnedObj.matter();
推荐阅读
- python - whois 包无法导入
- android - Android Gradle 读取超时
- jquery - 如何在 JQuery 计算器中应用算术运算与第一次计算的总值
- regex - find_all 用正则表达式在美丽的汤中
- google-cloud-platform - VM Instance 关闭后我的网站会丢失数据吗?
- python - 数据框的每一行都有一个列名..如何将其更改为一行名称?
- python - 使用 Python 2.7 从 HTML 字符串中提取文件名
- python - 我如何制作具有两个特定间隔的数组?
- firebase - firebase https url 会接收 http post 请求吗?
- javascript - 如何从数据库和分页显示特定 div 中的特定数据?