Joyride Posted June 12, 2008 Report Share Posted June 12, 2008 Ir pašrakstīta XHR wrappera klase (izņēmu ārā visu lieko kodu, lai nejauktu galvu): function Ajax() { var _this = this; // Šeit klasei varam piekļūt ar "this" function checkTimeout() { // ... } this.execute = function() { // Šeit klasei varam piekļūt ar "_this" interval_id = setInterval("???.checkTimeout();", 1000); // Problēma !!! } } Kā lai šeit norāda, kas ir owneris f-jai checkTimeout? "this" tiek uztverts kā parent f-ja "execute", "_this" ir "undefined"... Link to comment Share on other sites More sharing options...
v3rb0 Posted June 12, 2008 Report Share Posted June 12, 2008 function Ajax() { this.checkTimeout = function() { console.log("ir"); } this.execute = function() { interval_id = setInterval(function(me){return function(){me.checkTimeout();}}(this), 1000); // un nav problēma !!! } } new Ajax().execute(); Link to comment Share on other sites More sharing options...
Joyride Posted June 12, 2008 Author Report Share Posted June 12, 2008 v3rb0, paldies, strādā, bet vai varētu paskaidrot, kas tajā rindā notiek vai uzrakstīt kaut kā izvērstāk, savādāk es īsti nesaprotu... function(me) { return function() { me.checkTimeout(); } }(this) // wtf? Link to comment Share on other sites More sharing options...
v3rb0 Posted June 12, 2008 Report Share Posted June 12, 2008 closures tas saucās, http://www.jibbering.com/faq/faq_notes/closures.html#clClDo Link to comment Share on other sites More sharing options...
Recommended Posts