JavaScript单独的模块中传递数据

首先我们来看看这张图,让我们来思考一下!

下买我给出我的完整思路代码

html代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<input type="button" value="button1" id="button1" />
<input type="button" value="button2" id="button2" />
<script src="https://cdn.bootcss.com/jquery/1.8.1/jquery.min.js"></script>
<script src="test1.js"></script>
</body>
</html>

test1.js代码

(function ($) {

    var userName = "用户名称2";

    function getEntity() {
        alert(userName);
    }

    $('#button1').click(function () {
        refFun(getEntity);
    })


})(jQuery);



(function ($) {

    var userName = "用户名称1"

    function getEntity() {
        alert(userName);
    }

    $('#button2').click(function () {
        refFun(getEntity);
    })


})(jQuery);

function refFun(fnName) {
    if (typeof fnName == "function") {
        fnName();
    }
}

如果这篇文章对您有帮助,您可以打赏我

技术交流QQ群:15129679

 
 
 
 
原文地址:https://www.cnblogs.com/yeminglong/p/10689544.html