【问题标题】:How to translate this bit of Python to idiomatic Javascript如何将这部分 Python 翻译成惯用的 Javascript
【发布时间】:2011-08-02 15:28:15
【问题描述】:

到目前为止我的代码:

// The q constant of the Glicko system.
var q = Math.log(10) / 400;

function Player(rating, rd) {
    this.rating = rating || 1500;
    this.rd = rd || 200;
}

Player.prototype.preRatingRD = function(this, t, c) {
    // Set default values of t and c
    this.t = t || 1;
    this.c = c || 63.2;
    // Calculate the new rating deviation
    this.rd = Math.sqrt(Math.pow(this.rd, 2) + (Math.pow(c, 2) * t));
    // Ensure RD doesn't rise above that of an unrated player
    this.rd = Math.min(this.rd, 350);
    // Ensure RD doesn't drop too low so that rating can still change
    // appreciably
    this.rd = Math.max(this.rd, 30);
};
Player.prototype.g = function(this, rd) {
    return 1 / Math.sqrt(1 + 3 * Math.pow(q, 2) * Math.pow(rd, 2) / Math.pow(Math.PI, 2));
};
Player.prototype.e = function(this, p2rating, p2rd) {
    return 1 / (1 + Math.pow(10, (-1 * this.g(p2rd) * (this.rating - p2rating) / 400)));
};

我正在开发 Glicko rating system 的 JS/HTML 实现,并大量借鉴 pyglicko ——也就是说,完全抄袭了它。

它相当短(如果没有 cmets,可能不到 100 LoC),但我对我的翻译是否会起作用感到疑虑,因为老实说,我不知道 Javascript 范围界定和 this 是如何实际工作的。您可以在顶部的链接中查看我的内容。

但具体来说,我想知道您将如何在 Javascript 中表达这段 Python 代码。基本上_d2 在Player 的类定义中。

def _d2(self, rating_list, RD_list):
    tempSum = 0
    for i in range(len(rating_list)):
        tempE = self._E(rating_list[i], RD_list[i])
        tempSum += math.pow(self._g(RD_list[1]), 2) * tempE * (1 - tempE)
    return 1 / (math.pow(self._q, 2) * tempSum)

我的函数 e 和 g 是这样定义的,q 是一个常量:

Player.prototype.e = function(this, ratingList, rdList) {
    // Stuff goes here
}

【问题讨论】:

    标签: javascript python translation


    【解决方案1】:

    在 Javascript 中,您不需要显式传递 self(实际上,Python 是这里的“怪异”)

    Player.prototype.e = function(rating_list, RD_list){
        //replace "self" with "this" here:
        var tempSum = 0; //if you don't use the "var", tempSum will be a global
                         // instead of a local
        for(var i=0; i<rating_list.length; i++){ //plain old for loop - no foreach in JS
            var tempE = this._E( ... ); //note that in JS, just like in Python,
                                        //variables like this have function scope and
                                        //can be accessed outside the loop as well
            tempSum += Math.pow( ... ) //the Math namespace is always available
                                       //Javascript doesn't have a native module system
        }
        return (...);
    }
    

    这应该可以正常工作。 关于this,您需要了解的唯一棘手的事情是它非常混杂。这意味着这取决于您如何调用函数:

     obj.e(); //if you do a method-like call, the this will be set to obj
    

    但是,幕后没有魔法绑定。以下在 python 中有效,但 not 在 Javascript 中有效:

     f = obj.e
     f(); //looks like a normal function call. This doesn't point to obj
    

    【讨论】:

      猜你喜欢
      • 2018-09-09
      • 1970-01-01
      • 2011-09-05
      • 2021-03-06
      • 2021-02-20
      • 2011-04-23
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      相关资源
      最近更新 更多