一个对象的问题~

xiaowei8888 2012-02-03 10:15:37
(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对象
...全文
126 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
未知数 2012-02-04
  • 打赏
  • 举报
回复
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)的区别,而没有什么某属性存不存在的问题
xiaowei8888 2012-02-04
  • 打赏
  • 举报
回复
明白了 3Q
未知数 2012-02-04
  • 打赏
  • 举报
回复
还能有什么区别
前者是BigInteger的属性,后者是BigInteger.prototype的属性
只不过如果定义BigInter.protype.ZERO=1的话,所有用BigInteger构造出的对象都会有一个初始为1
的ZERO属性
xiaowei8888 2012-02-04
  • 打赏
  • 举报
回复
谢谢一楼,再问一下 BigInteger.ZERO和 BigInteger.Prototype.ZERO有什么区别么?
xiaowei8888 2012-02-03
  • 打赏
  • 举报
回复
就是说对象本身可以做自己的属性么 纠结= =

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧