gpt4 book ai didi

javascript - 将显示模块模式代码转换为模块模式

转载 作者:行者123 更新时间:2023-11-28 08:15:25 25 4
gpt4 key购买 nike

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

我正在浏览上面的杰作。我仍然无法从上面的文章中得到两件事。

var myRevealingModule = (function () {

var privateVar = "Ben Cherry",
publicVar = "Hey there!";

function privateFunction() {
console.log( "Name:" + privateVar );
}

function publicSetName( strName ) {
privateVar = strName;
}

function publicGetName() {
privateFunction();
}


// Reveal public pointers to
// private functions and properties

return {
setName: publicSetName,
greeting: publicVar,
getName: publicGetName
};

})();

myRevealingModule.setName( "Paul Kinlan" );

相关问题: JavaScript design pattern: difference between module pattern and revealing module pattern?

我从上面的问题中理解了很多部分。任何人都可以将上面的揭示模块模式转换为传统的模块模式,以便我可以直观地看到差异。

最佳答案

模块模式代码如下所示:您可以看到 jsfiddle

var myModule = (function () {

var privateVar = "Ben Cherry";

function privateFunction() {
console.log( "Name:" + privateVar );
}

return {
setName: function( strName ) {
privateVar = strName;
},
greeting: "Hey there!",
getName: function() {
privateFunction();
}
};

})();
myModule.setName( "Paul Kinlan" );

关于javascript - 将显示模块模式代码转换为模块模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com