【问题标题】:How I can Call Javascript prototype functions?如何调用 Javascript 原型函数?
【发布时间】:2011-09-23 21:50:56
【问题描述】:

这是要调用的 Javascript 代码 来自 Stage6 的功能警报,我将完全重建以使其恢复正常运行 :)

// Alerts (in the header)
    function Alerts(total) {
        this.current_page = 1;
        this.last_page = Math.ceil(total / 4);
        if (this.current_page == this.last_page) {
            $('alert-next-button').className = 'alert-next-inactive';
        }
    }

这是来自 DivX Stage6 的原型函数:

Alerts.prototype.next = function () {
    if (this.current_page == this.last_page) {
        return;
    }
    new Effect.BlindUp('alerts-' + this.current_page, {
        scaleFrom:80,
        duration:0.4,
        queue:{
            position:'end',
            scope:'alerts'
        }
    });
    new Effect.BlindDown('alerts-' + (this.current_page + 1), {
        scaleTo:80,
        duration:0.4,
        queue:{
            position:'end',
            scope:'alerts'
        }
    });
    this.current_page++;
    if (this.current_page > 1) {
        $('alert-prev-button').className = 'alert-prev';
    }
    if (this.current_page == this.last_page) {
        $('alert-next-button').className = 'alert-next-inactive';
    }
}

第二个原型函数:

Alerts.prototype.previous = function () {
    if (this.current_page == 1) {
        return;
    }
    new Effect.BlindUp('alerts-' + this.current_page, {
        scaleFrom:80,
        duration:0.4,
        queue:{
            position:'end',
            scope:'alerts'
        }
    });
    new Effect.BlindDown('alerts-' + (this.current_page - 1), {
        scaleTo:80,
        duration:0.4,
        queue:{
            position:'end',
            scope:'alerts'
        }
    });
    this.current_page--;
    if (this.current_page == 1) {
        $('alert-prev-button').className = 'alert-prev-inactive';
    }
    if (this.current_page < this.last_page) {
        $('alert-next-button').className = 'alert-next';
    }
}

我需要此功能的 HTML 代码。 这是逆向工程:=)

这是第 6 阶段的图片 http://img10.imageshack.us/img10/1733/83630697.png

我都测试过了,但我没有解决方案。

希望有人能帮帮我。

【问题讨论】:

  • 你觉得这里的原型函数是什么?
  • 请描述您正在尝试做什么。您是否尝试使用方法创建函数或持久对象?您对this 的使用通常与对象/方法相关联,但我看不到任何上下文。如果你只是想要一个函数,那么你可以将current_pagelast_page更改为局部变量并将其用作普通函数。
  • 你说的是Prototype.js而不是原型继承吗?

标签: javascript function-prototypes


【解决方案1】:

不确定您是要定义原型函数,还是调用原型函数:

制作原型函数:

Alerts.prototype.nameYourFunction = function(total) {
    this.current_page = 1;
    this.last_page = Math.ceil(total / 4);
    if (this.current_page == this.last_page) {
        $('alert-next-button').className = 'alert-next-inactive';
    }

};

nameYourFunction 替换为您想调用的任何名称

那么你就可以这样称呼它了:

Alerts.nameYourFunction(2);

或致电您添加到问题中的人:

Alerts.next();

【讨论】:

    【解决方案2】:

    嗯……嗯……var a = new Alerts(5); a.next()????

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多