小编Том*_*мов的帖子

Setter 和 Getter 错误

function Point () {
    this.xPos = 0;
    this.yPos = 0;
}

Object.__defineGetter__.call(Point.prototype, "getPoint", function(){
    return "X: " + this.xPos + " Y: " + this.yPos;
});

Object.__defineSetter__.call(Point.prototype, "setPoint", function(point){
    var parts = point.toString().split(', ');
    parts[0] = this.xPos;
    parts[1] = this.yPos;
});

var newPoint = new Point();

newPoint.setPoint("44.5, 60.6");

console.log(newPoint.getPoint);
Run Code Online (Sandbox Code Playgroud)

它返回一个错误: newPoint.setPoint 不是函数。不明白为什么你能帮助我?尝试处理 setter 和 getter。

javascript object

2
推荐指数
1
解决办法
151
查看次数

GCD超过2个数字

function gcd (a, b) {
    if(b == 0){
        return a;
    }
    return gcd(b, a%b);
}

function gcd_more_than_two_numbers (a) {
    var last = Math.max.apply(null, a);
    var first = Math.min.apply(null, a);    

    return gcd(first, last);
}

console.log(gcd_more_than_two_numbers([9999,213123,9,15,27])); 
console.log(gcd_more_than_two_numbers([5,10,15,25]));
Run Code Online (Sandbox Code Playgroud)

取数组中的最低和最高值并为它们之间的所有数字找到一个gcd是否正确?数学上正确吗?

javascript math

0
推荐指数
1
解决办法
299
查看次数

标签 统计

javascript ×2

math ×1

object ×1