Javascript 嵌套函数错误,意外标记';:';,无法读取未定义的属性

Javascript 嵌套函数错误,意外标记';:';,无法读取未定义的属性,javascript,namespaces,nested-function,javascript-namespaces,Javascript,Namespaces,Nested Function,Javascript Namespaces,我想让我的代码更清楚,这就是我试图学习名称空间或嵌套函数的原因。我在几个地方读过关于它们的文章,据我所知,“显示模块”是最好的选择。所以我试着从这篇文章中复制第二个例子 起初,我尝试使用两种方法返回对象时出错:意外标记“:”。当我尝试返回一个方法而不是2个方法时,我在尝试调用expandableElements对象的applyEvents时得到了undefined的Cannot read属性错误 let expandableElements = ( function() {

我想让我的代码更清楚,这就是我试图学习名称空间或嵌套函数的原因。我在几个地方读过关于它们的文章,据我所知,
“显示模块”
是最好的选择。所以我试着从这篇文章中复制第二个例子

起初,我尝试使用两种方法返回对象时出错:
意外标记“:”
。当我尝试返回一个方法而不是2个方法时,我在尝试调用
expandableElements
对象的
applyEvents
时得到了undefined的
Cannot read属性错误

let expandableElements = 
(
    function() 
    {
        // All expandable elements
        let expands = document.querySelectorAll(".expand-icon");

        // Apply events to all .expand-icon elements
        function applyExpandEvents()
        {
            for(let i = 0; i < expands.length; i++)
            {
                expands[i].addEventListener("click", expandList);
            }

            // Expand method
            function expandList() 
            {
                this.classList.toggle("active");
                let content = this.previousElementSibling;

                if (content.style.maxHeight)
                {
                    content.style.padding = "0";

                    content.style.maxHeight = null;
                } 
                else 
                {
                    content.style.padding = "1rem";
                    content.style.paddingBottom = "0.5rem";

                    content.style.maxHeight = content.scrollHeight + 20 + "px";
                } 
            }
        }


        // Close all expanded lists
        function closeAllExpands()
        {
            for(let i = 0; i < expands.length; i++)
            {
                let expand = expands[i];

                if(expand.classList.contains("active"))
                {
                    expand.classList.toggle("active");

                    expand.previousSibling.style.padding = "0";
                    expand.previousSibling.style.maxHeight = null;
                }
            }
        }

        return 
        {
            applyEvents : applyExpandEvents,
            closeAll    : closeAllExpands // Unexpected token ':'
        };
    }
)();

expandableElements.applyEvents(); // If remove closeAll from return, then'Cannot read property applyEvents of undefined'
let expandableElements=
(
函数()
{
//所有可扩展元件
让expans=document.queryselectoral(“.expans icon”);
//将事件应用于所有。展开图标元素
函数applyExpandEvents()
{
for(设i=0;i
返回的
和JSON必须在同一行。一旦返回行被执行,控制权就被移交给调用者(使用
未定义的
),JSON行永远不会被执行

这样做:

...
        return {
            applyEvents : applyExpandEvents,
            closeAll    : closeAllExpands // Unexpected token ':'
        };
详细解释来自:

return语句受自动分号插入(ASI)的影响。return关键字和表达式之间不允许使用行结束符

由ASI转化为:

return; 
a + b;

什么该死的lol?)我不喜欢第一次在同一条线上支撑,为什么这甚至是一个问题?)我已经解释了原因。请看一看
return; 
a + b;