Object scope in javascript (requirejs) [duplicate]

送分小仙女□ 提交于 2019-12-25 02:57:13

问题


I'm using requireJS with googlemaps. I'm using plugin to load google maps: https://gist.github.com/taktran/5389668 (google maps api should be loaded with jQuery promise)

I have problem with "this" scope. Here is my code:

//funcs.js file  
  define('myFuncs', ['jquery', 'googleMaps'], function ($, googleMaps) {
      var myFuncs = {
        myVar="true";
        init: function () {
          googleMaps(this.map_init);
        },
        map_init:function () {
          //scripts after loading map
          console.log(this.myVar); //undefined
          console.log(this);//jQuery promise
          console.log(myFuncs.myVar); //true
      }
      return myFuncs;
    });

execute:

require(['funcs'], function (myFuncs) {
  myFuncs.init();
});

How to make this=myFuncs not jQuery promise??

来源:https://stackoverflow.com/questions/23515964/object-scope-in-javascript-requirejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!