【问题标题】:Getting "This" into a namespace in Javascript将“This”放入 Javascript 中的命名空间
【发布时间】:2026-02-10 16:55:02
【问题描述】:

我确定这应该是一个简单的问题,但我仍在学习,所以就这样吧:

我有一些代码可以在点击时运行一个函数,将点击的元素的 ID 分配给一个变量,但我不知道如何在不创建全局变量的情况下将“this.id”值传递给命名空间(我认为很糟糕)。

<script>
    fsa = (function() {

        function GetTemplateLoc() {
            templateId = document.activeElement.id;
            alert(templateId + templateId2);
        }


        return {
            GetTemplateLoc: GetTemplateLoc,
        }

    })();

    //call the functions
    $(document).on('click', '.template', function () {
        fsa.GetTemplateLoc();
    });
</script>

和带有随机图片的 HTML:

<img id="template-1" class="template" src="http://fc02.deviantart.net/fs70/f/2010/028/c/b/cb21eda885b4cc6ee3f549a417770596.png"/>
<img id="template-2" class="template" src="http://fc02.deviantart.net/fs70/f/2010/028/c/b/cb21eda885b4cc6ee3f549a417770596.png"/>

【问题讨论】:

  • 那么明显的fsa.GetTemplateLoc(this.id);呢?
  • 该死的……当然。谢谢,正如你所知,我已经编程了几个星期了 :)

标签: javascript jquery namespaces this


【解决方案1】:

以下方法可行:

var fsa = (function() {
    function GetTemplateLoc() {
        var templateId = this.id;
        alert(templateId);
    }
    return {
        GetTemplateLoc: GetTemplateLoc,
    }
})();

//call the functions
$(document).on('click', '.template', fsa.GetTemplateLoc);

jQuery 通常调用您作为事件处理程序传递的函数,并将 this 设置为与事件关联的 DOM 对象。

在这种情况下,它将调用GetTemplateLoc(),并将this设置为.template元素,因此您可以直接在函数中使用this,而无需传递任何参数。

重要提示:始终使用var 声明变量。 JavaScript 对变量没有自动的函数局部作用域,即没有var 声明的每个变量都是全局的,无论你在哪里声明它。换句话说,忘记var 算作一个错误。

【讨论】:

  • 谢谢!附带说明一下,您刚刚修复了一些其他错误,因为我不知道没有 var 是全局的变量。
【解决方案2】:

试试这个:你可以直接使用this.id来传递被点击元素的id,其中this指的是被点击元素的实例。

<script>
    fsa = (function() {

        function GetTemplateLoc(templateId ) {
            //templateId = document.activeElement.id;
            alert(templateId + templateId2);
        }


        return {
            GetTemplateLoc: GetTemplateLoc,
        }

    })();

    //call the functions
    $(document).on('click', '.template', function () {
        fsa.GetTemplateLoc(this.id);
    });
</script>

【讨论】:

    【解决方案3】:

    如果您能够在 GetTemplateLoc 函数中使用 jQuery,您可以执行以下操作:

    var fsa = (function() {
    
        function GetTemplateLoc($trigger) {
            var templateId = $trigger.attr('id'),
                templateId2 = $($trigger.siblings('.template')[0]).attr('id');
            alert(templateId + ' ' + templateId2);
        }
    
    
        return {
            GetTemplateLoc: GetTemplateLoc,
        }
    })();
    
    $(document).on('click', '.template', function () {
        fsa.GetTemplateLoc($(this));
    });
    

    您可以将GetTemplateLoc设置为期望jQuery对象作为参数($trigger开头的美元符号可用于将其区分为jQuery对象而不是任何其他数据类型,这不是必需的,但可以有时帮助澄清事情)。

    templateId 将存储点击图像的 ID 值,templateId2 将存储另一个图像的 ID 值。我还在警报中的两个变量之间添加了一个空格。

    如果你不能在 GetTemplateLoc 中使用 jQuery,你可以这样做:

    var fsa = (function() {
    
        function GetTemplateLoc(trigger) {
            var templateId = trigger.id;
    
            var templateId2 = trigger.nextElementSibling == null ? trigger.previousElementSibling.id : trigger.nextElementSibling.id;
    
            alert(templateId + ' ' + templateId2);
        }
    
        return {
            GetTemplateLoc: GetTemplateLoc,
        }
    })();
    

    这一次,触发事件的.template被传递到GetTemplateLoc,但这次它不是一个jQuery对象。 templateId 分配给触发器的 ID,然后将 templateId2 分配到 ternary 中。首先,检查triggernextElementSibling 是否为null。如果是,我们知道trigger 是两个.template 元素中的第二个。因此我们可以将templateId2 设置为trigger 的前一个兄弟的ID。如果triggernextElementSibling 不是null,那么我们知道trigger 是第一个模板,我们用nextElementSibling 的ID 填充templateId2。这种确切的方法仅适用于两个.template,如果还有更多,您将需要一些额外/不同的逻辑,可能会检索所有.template ID,然后循环它们以将它们添加到警报消息中。希望这会有所帮助。

    【讨论】: