在javascript对象中克隆$(this)?

在javascript对象中克隆$(this)?,javascript,object,call,this,apply,Javascript,Object,Call,This,Apply,我正在学习如何使用对象来帮助组织我的代码并给它一些结构,但我遇到了一个问题。我不明白如何将一个函数内部的$(this)设置为另一个函数的$(this) 我正在研究call和apply,但我似乎无法理解它在这种情况下是如何工作的 cloneCard和clickCard是我遇到的问题。我想将单击卡片时引用的$(this)传递给cloneCard函数 以下是我迄今为止的代码(已更新以反映答案): var模态={ 初始化:函数(配置){ this.config=config; 点击卡片(); this.

我正在学习如何使用对象来帮助组织我的代码并给它一些结构,但我遇到了一个问题。我不明白如何将一个函数内部的$(this)设置为另一个函数的$(this)

我正在研究call和apply,但我似乎无法理解它在这种情况下是如何工作的

cloneCard和clickCard是我遇到的问题。我想将单击卡片时引用的$(this)传递给cloneCard函数

以下是我迄今为止的代码(已更新以反映答案):

var模态={
初始化:函数(配置){
this.config=config;
点击卡片();
this.removeModal();
单击此项。单击外部();
这个.createClose();
},
clickCard:function(){
$this=这个;
此.config.boardOutput.on('click','.card',函数(事件){
$this.showOverlay();
$this.cloneCard.call($(this));
$this.createClose();
});
},
cloneCard:function(){
this.clone()
.replaceWith($('').html(this.html()))
.removeClass('card')
.addClass('modal')
.css(“页边距顶部”,$(窗口).scrollTop()
.prependTo(“主体”);
},
showOverlay:function(){
this.config.overlay.show();
},
removeModal:function(){
$('.modal').remove();
$('.overlay').hide();
},
单击外部:函数(){
this.config.overlay.on('click',this.removeModel);
},
createClose:function(){
$('X')
.prependTo(“.modal”)
.on('click',this.removeModel);
}
};
Modal.init({
boardOutput:$(“#board output”),
覆盖:$(“.overlay”)
});

如需所需,请调用self.cloneCard.call($(this))而不是
self.cloneCard($(this));
应该可以。您要做的是调用cloneCard,并将clickCard事件发生的元素传递给它


如果这不起作用,我想我们需要更多信息来解决您的问题。

谢谢您的回复。显然我做得很好,但是我忘了在replaceWith方法中使用新的$this。谢谢你报告的不是问题所在。这一行就是问题所在。
.replaceWith($('').html($(This.html())
。。哦,你发现自己也可以这样做,而且似乎比不使用call要好。还有,现在我明白了呼叫的工作原理!谢谢(对于任何感兴趣的人来说,它似乎“更好”,因为它不需要传入$(this)然后在另一个方法中将其作为变量引用。$(this)现在是调用后的this。太棒了!
var Modal = {
            init: function(config) {
                this.config = config;
                this.clickCard();
                this.removeModal();
                this.clickOutside();
                this.createClose();
            },
            clickCard: function() {
                $this = this;
                this.config.boardOutput.on('click', '.card', function(event) {
                    $this.showOverlay();
                    $this.cloneCard.call($(this));
                    $this.createClose();
                });
            },
            cloneCard: function() {
                this.clone()
                    .replaceWith($('<div/>').html(this.html()))
                    .removeClass('card')
                    .addClass('modal')
                    .css("margin-top", $(window).scrollTop())
                    .prependTo('body');
            },
            showOverlay: function() {
                this.config.overlay.show();
            },
            removeModal: function() {
                $('.modal').remove();
                $('.overlay').hide();
            },
            clickOutside: function() {
                this.config.overlay.on('click', this.removeModal);
            },
            createClose: function() {
                $('<span class="close">X</span>')
                    .prependTo('.modal')
                    .on('click', this.removeModal);
            }
        };

        Modal.init({
            boardOutput: $('#board-output'),
            overlay: $('.overlay')
        });