如何通过自己的功能改变对象的属性?

时间:2016-08-12 16:11:24

标签: javascript jquery oop

这是我自己的js文件。

function hesap (hesapdiv, hesapmodal, hesapid){
    this.hesapid = hesapid;
    this.hesapdiv = hesapdiv;
    this.hesapmodal = hesapmodal;

    hesapdiv.on("click", "button", function() {
      hesapmodal.modal("show");
    });


    hesapmodal.on("click", "button[hesapid]", function() {
    var hesapid = $(this).attr('hesapid');
    console.log(this.hesapid + "-" + hesapid + "-" + $(this).attr('hesapid'));
    var isim = $(this).attr('isim');
    hesapdiv.find("input").val(hesapid + " - " + isim);
    hesapmodal.modal('hide');
  });}

以下代码,我可以在我的所有网页中使用。

var hesap1 = new hesap($("#hesapdiv"), $("#HesaplarModal"), 36);
$("#kasaislemikaydet").on('click', function(event) {
        event.preventDefault(); // To prevent following the link (optional)
        alert(hesap1.hesapid);)};

现在,当用户从模态中选择“hesap”时,函数成功获取属性值。但它没有将值赋给对象的'hesapid'。所以从网页上我无法获得hesapid的新价值。例如,单击按钮以获取新选择的'hesapid'的值,始终警告第一个值'36'。

2 个答案:

答案 0 :(得分:0)

var _this=this; //save this to variable   

hesapmodal.on("click", "button[hesapid]", function() {

     console.log(_this.hesapid);//here You have object property and it can be changed
     //example assign
     _this.hesapid = $(this).attr('hesapid');

});

我正在创建 _this 变量,因为Click事件回调中的 this 是调用了事件的DOM元素,因此为 this (hesap对象)在 _this 变量中,该变量在回调函数中可见,可以在那里使用。

完整的工作代码:

function hesap (hesapdiv, hesapmodal, hesapid){

    this.hesapid = hesapid;
    this.hesapdiv = hesapdiv;
    this.hesapmodal = hesapmodal;

    var _this=this;//to use this in click callback

    this.hesapdiv.on("click", "button", function() {

        _this.hesapmodal.modal("show");
    });


    this.hesapmodal.on("click", "button[hesapid]", function() {

        _this.hesapid = $(this).attr('hesapid');

        var isim = $(this).attr('isim');

        _this.hesapdiv.find("input").val( _this.hesapid + " - " + isim);

        _this.hesapmodal.modal('hide');

    });

}

答案 1 :(得分:0)

$validator = new \himiklab\yii2\recaptcha\ReCaptchaValidator;
$validator->secret = '...';

if ($validator->validate($entered_recaptcha_code, $error)) {
    // ok
} else {
    echo $error;
}
javascript中的

function hesap (_hesapdiv, _hesapmodal, _hesapid){ var self = this; this.hesapid = _hesapid; this.hesapdiv = _hesapdiv; this.hesapmodal = _hesapmodal; hesapdiv.on("click", "button", function() { self.hesapmodal.modal("show"); }); hesapmodal.on("click", "button[hesapid]", function() { var hesapid = $(this).attr('hesapid'); console.log(self.hesapid + "-" + hesapid + "-" + $(this).attr('hesapid')); var isim = $(this).attr('isim'); self.hesapdiv.find("input").val(hesapid + " - " + isim); self.hesapmodal.modal('hide'); });} 指的是this所在的函数。

ES6有更好的方法来处理这个

this

使用function hesap (hesapdiv, hesapmodal, hesapid){ this.hesapid = hesapid; this.hesapdiv = hesapdiv; this.hesapmodal = hesapmodal; hesapdiv.on("click", "button", () => { hesapmodal.modal("show"); }); hesapmodal.on("click", "button[hesapid]", () => { var hesapid = $(this).attr('hesapid'); console.log(this.hesapid + "-" + hesapid + "-" + $(this).attr('hesapid')); var isim = $(this).attr('isim'); hesapdiv.find("input").val(hesapid + " - " + isim); hesapmodal.modal('hide'); }); } 代替ES6中的() =>进行购买function ()返回到父this。在您的情况下,function ()函数。