0

widget内でスコープが失われるという問題があります。_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…}
4

2 に答える 2