javascript原型类,在jquery中单击

javascript原型类,在jquery中单击,javascript,jquery,click,prototype,this,Javascript,Jquery,Click,Prototype,This,我制作了一个javascript原型类 在一个方法中,我创建了一个jqueryclick。 但是在这个点击中,我想执行我的构建函数 当我尝试在jquery中执行原型函数时,单击它会失败,因为jquery将其用于其他用途 我尝试了一些不同的方法,但没能奏效 Game.prototype.clicks = function(){ $('.flip').click(function(){ if(cardsPlayed.length < 2) //minder dan 2

我制作了一个javascript原型类

在一个方法中,我创建了一个jqueryclick。 但是在这个点击中,我想执行我的构建函数

当我尝试在jquery中执行原型函数时,单击它会失败,因为jquery将其用于其他用途

我尝试了一些不同的方法,但没能奏效

Game.prototype.clicks = function(){
    $('.flip').click(function(){

        if(cardsPlayed.length < 2) //minder dan 2 kaarten gespeeld
        {
            $(this).find('.card').addClass('flipped');
            cardsPlayed.push($(this).find('.card').attr('arrayKey'));

            console.log(cardsPlayed[cardsPlayed.length - 1]);

            console.log(playingCards[cardsPlayed[cardsPlayed.length - 1]][0]);

            if(cardsPlayed.length == 2)// two cards played
            {
                if(playingCards[cardsPlayed[0]][0] == playingCards[cardsPlayed[1]][0])
                { // same cards played
                    console.log('zelfde kaarten');
                    playingCards[cardsPlayed[0]][0] = 0; //hide card one
                    playingCards[cardsPlayed[1]][0] = 0; //hide card two
                    //rebuild the playfield
                    this.build(); //error here
                }
                else
                {
                    //differend cards
                }

            }
        }

        return false;
    }).bind(this);
}
Game.prototype.clicks=function(){
$('.flip')。单击(函数(){
if(cardsPlayed.length<2)//minder dan 2 kaarten gespeeld
{
$(this.find('.card').addClass('fliped');
cardsPlayed.push($(this.find('.card').attr('arrayKey'));
log(cardsPlayed[cardsPlayed.length-1]);
log(playingCards[cardsPlayed[cardsPlayed.length-1]][0]);
如果(cardsPlayed.length==2)//打了两张牌
{
如果(玩卡[cardsPlayed[0]][0]==玩卡[cardsPlayed[1]][0])
{//玩的是同样的牌
console.log('zelfde kaarten');
playingCards[cardsPlayed[0][0]=0;//隐藏第一张卡
玩牌[cardsPlayed[1][0]=0;//隐藏牌二
//重建运动场
this.build();//此处出错
}
其他的
{
//不同的卡片
}
}
}
返回false;
}).约束(本);
}

如果我理解正确,在现代浏览器中,您只需使用
bind

function MyClass() {
  this.foo = 'foo';
  $('selector').each(function() {
    alert(this.foo); //=> 'foo'
  }.bind(this));
}

否则,只需将
这个
缓存在一个变量中,通常是
self
,并在必要时使用它。

我想您需要这样的东西:

function class(){
    var self = this;
    this.build = function(){};
    $('#element').click(function(){
        self.build();
    });
};

问题是您试图让
引用
$(this.find('.card')
中单击的
.flip
元素以及
此.build()中的
游戏
对象
不能具有双重人格,因此其中一个引用需要更改

最简单的解决方案,正如李森所建议的,是在
点击
处理程序的作用域中保留一个指向
游戏
对象的变量。然后,只需在处理程序中为单击的元素使用
this
(在jQuery处理程序中通常如此),并为
Game
对象使用
self

Game.prototype.clicks = function() {
    // Keep a reference to the Game in the scope
    var self = this;

    $('.flip').click(function() {
        if(cardsPlayed.length < 2) //minder dan 2 kaarten gespeeld
        {
            // Use this to refer to the clicked element
            $(this).find('.card').addClass('flipped');
            // Stuff goes here...
            // Use self to refer to the Game object
            self.build();
        }
    }); // Note: no bind, we let jQuery bind this to the clicked element
};
Game.prototype.clicks=function(){
//在范围内保留对游戏的引用
var self=这个;
$('.flip')。单击(函数(){
if(cardsPlayed.length<2)//minder dan 2 kaarten gespeeld
{
//使用此选项可引用单击的元素
$(this.find('.card').addClass('fliped');
//这里有东西。。。
//使用self引用游戏对象
self.build();
}
});//注意:没有绑定,我们让jQuery将其绑定到单击的元素
};

问题是什么?代码在哪里?用代码编辑我的帖子时没有注意到添加绑定时有任何不同(这一点),sorry@JasperFioole当前位置仔细查看我的代码,然后查看你的代码<代码>绑定
用于函数。我在代码中的一些不同位置尝试过它,但没有显示任何差异。我在问题中添加了代码。你能告诉我放在哪里吗?我知道你的意思,我试过了,这在:console.log(playingCards[cardsPlayed[cardsPlayed.length-1][0]);-未捕获的TypeError:无法读取未定义的属性“0”,这可能是因为我也使用了jquery的this变体:cardsPlayed.push($(this).find('.card').attr('arrayKey');问题是您试图使
this
成为
$(this.find)('.card')
中的jQuery对象,以及
this.build()中的
游戏
对象。即使是
.bind()
也帮不了你。我尝试了一些类似的东西,但不幸的是,我无法让它工作。