亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想在js里實現類的封裝,但遇到一個問題。具體的請看代碼。問題見注釋

想在js里實現類的封裝,但遇到一個問題。具體的請看代碼。問題見注釋

子衿沉夜 2023-04-14 12:08:34
var TestClass;if (TestClass == undefined) {  TestClass = function(){   } }TestClass.prototype.init = function (options) { $(".alert_click").click(function(){ this.popup("test");                //這樣的代碼會提示popup未定義。如果想在此處調用popup應該怎么做? }); };TestClass.prototype.popup = function (value) { alert(value); } $(function(){ var testClass = new TestClass(); testClass.init(); }
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

var TestClass;if (TestClass == undefined) {  TestClass = function(){
  }
}	TestClass.prototype.init = function (options) {		var self = this;
        $(".alert_click").click(function(){
                self.popup("test");//訪問閉包里的this
                //這樣的代碼會提示popup未定義。如果想在此處調用popup應該怎么做?
        });
	};	TestClass.prototype.popup = function (value) {		    alert(value);
	}

                        
	$(function(){        var testClass = new TestClass();
        testClass.init();
	});


查看完整回答
反對 回復 2023-04-17
?
www說

TA貢獻1775條經驗 獲得超8個贊

var person = {  
name: "Alex Russell",
hello: function() { console.log(this.name + " says hello world"); }
}

$("#some-div").click(person.hello.bind(person));

// when the div is clicked, "Alex Russell says hello world" is printed


查看完整回答
反對 回復 2023-04-17
  • 2 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號