试图制作一个模块.为什么功能不确定



我只是在努力更好地理解JavaScript中的匿名功能和模块。我想知道为什么"测试"不确定?这是我的HTML和JavaScript:

html:

<div class="box">
    <p>One</p>
    <p>Two</p>
    <p>Three</p>
</div>

javaScript:

(function (el) {
    function test () {
        this.el = el;
        console.log(this.el);
    };
    return test;
}(document.querySelector('.box')));
(function () {
    new test();
}());

如果您想访问测试,则可以这样做:

var test = (function (el) {
    return function() {
        this.el = el;
        console.log(this.el);
    };
}(document.querySelector('.box')));
(function () {
    new test();
}());

最新更新