1

以下のような機能があります。

私はこれをで呼ぼうとしています

var testVar =  new genericYiiLocation();
console.log(testVar.getLongitude());

ただし、this.getLongitude()には常に空のthis.longitudeがあります。

this.longitudeに、locateSuccess(loc)で設定したときに期待どおりの値が含まれていることを確認しましたが、問題ないようです。

任意のガイダンスをいただければ幸いです。

function genericYiiLocation() {

    console.log('genericYiiLocation: Creating location handler');

    this.longitude=  '';
    this.latitude=   '';
    this.accuracy=   '';

    if (Modernizr.geolocation) {
        console.log('genericYiiLocation: Location supported');
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);
        }
        else {
            alert('genericYiiLocation: Geolocation is not supported in your current browser.');
            return false;
        }
    } else {
        alert ('genericYiiLocation: no native location support');
        return false;
    }


     function locateSuccess(loc){
        console.log('genericYiiLocation: storing location data');
        this.longitude = loc.coords.longitude;
    }

    // Unsuccessful geolocation
    function locateFail(geoPositionError) {
        switch (geoPositionError.code) {
            case 0: // UNKNOWN_ERROR
                alert('An unknown error occurred, sorry');
                break;
            case 1: // PERMISSION_DENIED
                alert('Permission to use Geolocation was denied');
                break;
            case 2: // POSITION_UNAVAILABLE
                alert('Couldn\'t find you...');
                break;
            case 3: // TIMEOUT
                alert('The Geolocation request took too long and timed out');
                break;
            default:
        }
    }

    this.getLongitude = function(){
        console.log('long: '+this.longitude);
        return this.longitude;
    }
}
4

2 に答える 2

1

私が理解している限り、その理由は次のとおりです。

コールバックのthis内側は、コールバックlocateSuccessの外側とは異なりthisます。意図したことを達成するために、コールバックlocalSuccess&locateFailthisusingにバインドできますFunction.prototype.bind

于 2013-01-13T04:45:56.637 に答える
0

次の行:

            navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);

関数がページのグローバルスコープでlocateSuccess実行されます。したがって、関数が にアクセスしようとすると、オブジェクトではなくオブジェクトを参照します。locateSuccessthiswindowgenericYiiLocation

genericYiiLocationこれを修正する簡単な方法は、オブジェクトをスコープ内の別の変数にバインドし、locateSuccessクロージャー内で次のように参照することです。

function genericYiiLocation() {

    console.log('genericYiiLocation: Creating location handler');

    this.longitude=  '';
    this.latitude=   '';
    this.accuracy=   '';

    //  ADDED
    var me = this;

    if (Modernizr.geolocation) {
        console.log('genericYiiLocation: Location supported');
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);
        }
        else {
            alert('genericYiiLocation: Geolocation is not supported in your current browser.');
            return false;
        }
    } else {
        alert ('genericYiiLocation: no native location support');
        return false;
    }


    function locateSuccess(loc){
        console.log('genericYiiLocation: storing location data');
        //  CHANGED
        me.longitude = loc.coords.longitude;
    }

    // Unsuccessful geolocation
    function locateFail(geoPositionError) {
        switch (geoPositionError.code) {
            case 0: // UNKNOWN_ERROR
                alert('An unknown error occurred, sorry');
                break;
            case 1: // PERMISSION_DENIED
                alert('Permission to use Geolocation was denied');
                break;
            case 2: // POSITION_UNAVAILABLE
                alert('Couldn\'t find you...');
                break;
            case 3: // TIMEOUT
                alert('The Geolocation request took too long and timed out');
                break;
            default:
        }
    }

    this.getLongitude = function(){
        console.log('long: '+this.longitude);
        return this.longitude;
    }
}
于 2013-01-13T04:43:35.710 に答える