赋值给变量返回undefined

时间:2014-03-27 08:16:04

标签: javascript undefined

我正在学习法语教程并且我遇到了一个未定义的变量,也许我错过了一些东西,似乎将值分配给变量但我无法访问它可能是一个范围问题?。

function Tileset(url) {
    // Chargement de l'image dans l'attribut image
    this.image = new Image();
    this.image.referenceDuTileset = this;
    this.image.onload = function() {
        if(!this.complete) 
            throw new Error("Erreur de chargement du tileset nommé \"" + url + "\".");

        // Largeur du tileset en tiles
        this.referenceDuTileset.largeur = this.width / 32;
    }

    this.image.src = "tilesets/" + url;
}

Tileset.prototype.dessinerTile = function(numero, context, xDestination, yDestination) {
    var xSourceEnTiles = numero % this.largeur;
    console.log(this);
    if(xSourceEnTiles == 0) xSourceEnTiles = this.largeur;
    var ySourceEnTiles = Math.ceil(numero / this.largeur);
    var xSource = (xSourceEnTiles - 1) * 32;
    var ySource = (ySourceEnTiles - 1) * 32;
    context.drawImage(this.image, xSource, ySource, 32, 32, xDestination, yDestination, 32, 32);
}

console.log(this)的输出:

-> Tileset {image: img, dessinerTile: function}
-> image: img 
largeur: 6
-> __proto__: Tileset

console.log(this.largeur)的输出

undefined

0 个答案:

没有答案