【问题标题】:Set variable within another context在另一个上下文中设置变量
【发布时间】:2012-02-09 02:44:06
【问题描述】:

我有以下功能

"use strict";
function Player
{
    this.width;
    this.height;
    this.framesA = 5;

    this.image = new Image();
    this.image.onload = function ()
    {
        width = this.width;
        height = this.height / framesA;
    }
    this.image.src = "Images/angel.png";
}

如何使此代码正常工作?
我希望在调用 onload 函数时将 Player 函数中的宽度和高度与图像的宽度和高度结合起来。

我需要这个才能在严格模式下工作(必须)。

如果这应该以其他方式完成,请随时教。

编辑:我更新了代码以反映更现实的情况(我不知道这会这么复杂)

编辑 2: 代码的另一个更新。我没有注意到我写了 var insted 这个。对不起。

提前致谢

【问题讨论】:

  • 只需使用this.image.widththis.image.height...
  • 你的例子应该可以工作,我想,有什么问题?
  • @Šime Vidas 我需要保存该值,以便获得解决方案

标签: javascript image syntax onload strict


【解决方案1】:

我从您的 cmets 收集到的是,您希望能够从 Player 函数外部访问宽度和高度,问题是您不知道宽度和高度何时可用。

如果这是正确的,它会变得有点复杂。由于您不知道何时加载图像并且在加载之前不能具有宽度和高度(除非您在img标签服务器端指定它们),您需要使用一个接受回调的函数访问播放器的宽度和高度。

基本上,如果宽度和高度未知,该函数只是将回调放入队列中。确定宽度和高度后,将调用队列中的所有函数,并将宽度和高度作为参数传递给它们。如果在调用函数时已经知道维度,则它应该立即使用正确的参数调用回调函数。

我会这样做:

function Player() {
    'use strict';

    // Store this in a variable so the onload handler can see it.
    var that = this;
    var callbacks = [];

    this.frames = 5;

    this.getDimensions = function (callback) {
        // We don't have the dimensions yet, so put the callback in the queue.
        callbacks.push(callback);
    };

    this.image = new Image();
    this.image.onload = function () {
        var width = this.width;
        var height = this.height / that.frames;

        // Call each of the registered callbacks.
        var i;
        for (i = 0; i < callbacks.length; i += 1) {
            callbacks[i](width, height);
        }
        // Don't keep unnecessary references to the functions.
        callbacks = null;

        // We now know the dimensions, so we can replace the getDimensions
        // function with one that just calls the callback.
        that.getDimensions = function (callback) {
            callback(width, height);
        };
    };
    this.image.src = "Images/angel.png";
}

以下是访问维度的方法:

var p = new Player();
p.getDimensions(function (width, height) {
    console.log("Player's width is " + width + " and height is " + height);
});

【讨论】:

  • 只有 1 个问题:var that = this 被复制(按值)还是内存地址被复制(按引用)?
  • @RandallFlagg、thatthis 指向同一个对象,它不会被复制。
【解决方案2】:

此变体通过 JSLint 没有任何问题,并且运行良好。那么有什么问题呢?

function Player() {
    "use strict";
    var width, height;
    this.image = new Image();
    this.image.onload = function () {
        width = this.width;
        height = this.height;
    };
    this.image.src = "Images/angel.png";
}

请注意,您应该放置“use strict”;内部功能而不是外部。

【讨论】:

  • A.我知道这是通过 JSLint B 进行的。我所有的代码都是严格的,所以没有必要将它放在函数中,因为它在整个文件 C 中使用。问题是如果你在支持严格模式的浏览器中测试它(我在 Chromium 上测试)你会看到宽度和高度是未定义的。
  • 对不起,我误导了你。 width 和 height 是 Player 的属性,没有定义为 var。我更新了我的代码。
【解决方案3】:

只需在onload 事件中设置变量并进行操作,它们就会保持在范围内。

"use strict";
function Player
{
    this.image = new Image();
    this.image.src = "Images/angel.png";
    this.image.onload = function ()
    {
        var width = this.width;
        var height = this.height;
        // Do your manipulations within the `onload` event.
    } 
}

【讨论】:

  • 哎呀,绝对不是OP要求的,会有多个玩家,你不能分享宽度
  • 您的第二个示例不起作用,因为当您调用 width = this.image.width 时,图像不会被加载(除非它被缓存)。要使这种方法起作用,您必须等待 onload 事件。
  • @Juan,你是对的!我最初将Player 误认为是一种标准方法。我修复了使用onload 事件并从内部执行操作的唯一解决方案。
  • JavaScript 允许您在外部函数中声明变量并在内部函数中使用它们:(function () { var a; (function () { a = 3; }()); window.alert(a); }())(警报3)。所以范围界定不是问题。
  • 这是一个作用域问题,因为当调用 onload 时作用域会移动到图像并且它不在播放器内
【解决方案4】:

这就是 Šime Vidas 的真正含义

"use strict";
function Player {
    this.image = new Image();
    this.image.src = "Images/angel.png";
}

Player.prototype.getWidth = function() {
    return this.image.width;
}

或者如果你喜欢基于闭包的对象

function Player {
    this.image = new Image();
    this.image.src = "Images/angel.png";
    this.getWidth = function() {
      return this.image.width;
    }
}

【讨论】:

  • 不,你不会......它只是在加载之前不可用。就像您的情况一样,width 在加载图像之前不会被定义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
相关资源
最近更新 更多