我在小部件内失去范围时遇到问题。在 _initialize 内部,this.options
不起作用,that.options
. 如何从此方法访问小部件的范围?
$.widget("myproj.map", {
options: {
centerLat: 51.511214,
centerLong: -0.119824,
},
// the constructor
_create: function () {
var that = this;
google.maps.event.addDomListener(window, 'load', this._initialize);
},
_initialize: function () {
var mapOptions = {
center: new google.maps.LatLng(that.options.centerLat, that.options.centerLong),
zoom: 11,
};
澄清
$.widget("myproj.map", {
options: {
centerLat: 51.511214,
centerLong: -0.119824,
},
// the constructor
_create: function () {
google.maps.event.addDomListener(window, 'load', this._initialize);
},
_initialize: function () {
console.log(this);
var mapOptions = {
center: new google.maps.LatLng(this.options.centerLat, this.options.centerLong),
zoom: 11
}; ...code omitted
不起作用,当我在 _initialize 中 console.log 时,我得到
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}