javascript - Using call to pass context -


i'm trying use call pass context of utilities object can access members (alldata array, etc) within mytest function.

i'm getting error:

referenceerror: alldata not defined

this tells me context lost , guess i'm not binding context correctly. how do this?

var utilities = {     alldata : [],     storage : [],      apirequest : function () {         this.alldata = ["a","b","c"];         var mytest = this.alldata.map.call(this, function (i, el) {             var url = 'testphp.php/translate_tts?ie=utf-8&tl=zh-cn&q=' + i;             return $.get(url, function (returned_data) {                 this.storage.push(returned_data);             });         });          $.when.apply($, mytest).done(function () {             log('done!');             log(this.storage[i]);         }); 

great answer above. 1 thing emphasize @ point: whenever bind object @ initialization bound , cannot call/apply'd using context anymore. imo it's whether use call/apply (at runtime) or .bind (permanently).

going here:

i'm trying use call pass context of utilities object can access members (alldata array, etc) within mytest function.

var othertest = function() {     this.datastorage.push(["ok"]);     console.log(arguments); }  var utilities = {     datastorage: [],     alldata: [],     apirequest: function() {         this.alldata = ["a","b","c"];          othertest.apply(this,arguments);      } }   utilities.apirequest('hu','hu'); console.log(utilities.datastorage[0]); 

since this reference object, can mutated @ time after initialization making easy use call/apply pass context which utilities object in case.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -