日期:2014-05-16  浏览次数:20448 次

一个对象的问题~
JScript code
(public) Constructor 
function BigInteger(a, b, c) {
    if (a != null) if ("number" == typeof a) this.fromNumber(a, b, c);
    else if (b == null && "string" != typeof a) this.fromString(a, 256);
    else this.fromString(a, b);
}

// return bigint initialized to value 
function nbv(i) {
    var r = nbi();
    r.fromInt(i);
    return r;
}

// return new, unset BigInteger 
function nbi() {
    return new BigInteger(null);
}

// (protected) set from string and radix 
function fromString(s, b) {
    var k;
    if (b == 16) k = 4;//k = 4
    else if (b == 8) k = 3;
    else if (b == 256) k = 8; // byte array 
    else if (b == 2) k = 1;
    else if (b == 32) k = 5;
    else if (b == 4) k = 2;
    else {
        this.fromRadix(s, b);
        return;
    }
    this.t = 0;
    this.s = 0;
    var i = s.length,
    mi = false,
    sh = 0;
    while (--i >= 0) {
        var x = (k == 8) ? s[i] & 0xff: intAt(s, i);
        if (x < 0) {
            if (s.charAt(i) == "-") mi = true;
            continue;
        }
        mi = false;
        if (sh == 0) this[this.t++] = x;
        else if (sh + k > this.DB) {
            this[this.t - 1] |= (x & ((1 << (this.DB - sh)) - 1)) << sh;
            this[this.t++] = (x >> (this.DB - sh));
        } else this[this.t - 1] |= x << sh;
        sh += k;
        if (sh >= this.DB) sh -= this.DB;
    }
    if (k == 8 && (s[0] & 0x80) != 0) {
        this.s = -1;
        if (sh > 0) this[this.t - 1] |= ((1 << (this.DB - sh)) - 1) << sh;
    }
    this.clamp();
    if (mi) BigInteger.ZERO.subTo(this, this);
}

function fromInt(x) {
    this.t = 1;
    this.s = (x < 0) ? -1 : 0;
    if (x > 0) this[0] = x;
    else if (x < -1) this[0] = x + DV;
    else this.t = 0;
}

BigInteger.ZERO = nbv(0);
BigInteger.ONE = nbv(1);

JScript code// 


rsa加密中遇到的 代码没有问题 就是有个地方看不明 请高手指点 3Q:
BigInteger.ZERO = nbv(0); 这行是什么意思 ZERO是BigInteger的属性?可是为什么nbv(0)返回的是一个BigInteger对象


------解决方案--------------------
BigInteger.ZERO = nbv(0);
BigInteger.ONE = nbv(1);

两种理解:
1,由于JS的弱类型,BigInteger也可以看做一个对象,这两句给对象BigInteger添加了两个属性
2,等效理解,给BigInteger类添加了两个静态属性,ZERO和ONE

至于nbv(0)返回BigInteger对象,那是当然的:
function nbv(i) {
var r = nbi();//调用nbi函数,自己看那个函数,nbi返回的就是new BigInteger(null);
r.fromInt(i);
return r;//再返回r,r是new BigInteger(null)

}

// return new, unset BigInteger 
function nbi() {
return new BigInteger(null);
}

至于你在一楼的疑问,是的
因为js是弱类型语言,灵活性非常强,可以在外部给对象添加属性和方法
var obj=new Object();
obj.a=1;
obj.test=function(){alert(obj.a);}
obj.test();
都是被允许的.
对于一个非空对象,你可以认为它的某属性只有已赋值(具有有效值)和未赋值(值为undefind)的区别,而没有什么某属性存不存在的问题