首页 > 解决方案 > 如何在字典中打印以及 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

解决方案


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();


推荐阅读