主题:  constructor?

sywh

职务:普通成员
等级:1
金币:0.0
发贴:66
#12003/10/31 8:37:46
在看源代码的时候看到constructor命令,只知道它和类有关系,不知道用法,查帮助也找不到,源码如下:
Vector3d.prototype.reset = function (x, y, z) {
this.constructor (x, y, z);
};
请问:constructor有什么用?



UndeadCraft

职务:版主
等级:4
金币:10.0
发贴:1993
#22003/10/31 11:00:47
把源文件贴上来吧,我也不太清楚,可能是自定义的function



sywh

职务:普通成员
等级:1
金币:0.0
发贴:66
#32003/10/31 11:26:36
ok,有点长,是做3D的东西
/*
Vector3d Class
Oct. 29, 2002
(c) 2002 Robert Penner

This is a custom object designed to represent vectors and points
in three-dimensional space. Vectors can added together,
scaled, rotated, and otherwise manipulated with these methods.

Dependencies: Math.sinD(), Math.cosD(), Math.acosD() (included below)

Discussed in Chapter 5 of
Robert Penner's Programming Macromedia Flash MX

www.robertpenner.com/profmx
www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
*/

/*
These three trigonometric functions are required for Vector
The full set of these functions is in trig_functions_degrees.as
*/
Math.sinD = function (angle) {
    return Math.sin (angle * (Math.PI / 180));
};

Math.cosD = function (angle) {
    return Math.cos (angle * (Math.PI / 180));
};

Math.acosD = function (ratio) {
    return Math.acos (ratio) * (180 / Math.PI);
};

/////////////////////////////////////////////////////////////

_global.Vector3d = function (x, y, z) {
this.x = x;
this.y = y;
this.z = z;
};

Vector3d.prototype.toString = function () {
var x = Math.round (this.x * 1000) / 1000;
var y = Math.round (this.y * 1000) / 1000;
var z = Math.round (this.z * 1000) / 1000;
return "[" + x + ", " + y + ", " + z + "]";
};

Vector3d.prototype.reset = function (x, y, z) {
this.constructor (x, y, z);
};

Vector3d.prototype.getClone = function () {
with (this) return new constructor (x, y, z);
};

Vector3d.prototype.equals = function (v) {
with (this) return (x == v.x && y == v.y && z == v.z)
};

Vector3d.prototype.plus = function (v) {
with (this) {
x += v.x;
y += v.y;
z += v.z;
}
};

Vector3d.prototype.plusNew = function (v) {
with (this) return new constructor (x + v.x, y + v.y, z + v.z);
};

Vector3d.prototype.minus = function (v) {
with (this) {
x -= v.x;
y -= v.y;
z -= v.z;
}
};

Vector3d.prototype.minusNew = function (v) {
with (this) return new constructor (x - v.x, y - v.y, z - v.z);
};

Vector3d.prototype.negate = function () {
with (this) {
x = -x;
y = -y;
z = -z;
}
};

Vector3d.prototype.negateNew = function () {
with (this) return new constructor (-x, -y, -z);
};

Vector3d.prototype.scale = function (s) {
with (this) {
x *= s;
y *= s;
z *= s;
}
};

Vector3d.prototype.scaleNew = function (v) {
with (this) return new constructor (x * v.x, y * v.y, z * v.z);
};

Vector3d.prototype.getLength = function () {
with (this) return Math.sqrt (x*x + y*y + z*z);
};

Vector3d.prototype.setLength = function (len) {
    var r = this.getLength();
    if (r) this.scale (len / r);
    else this.x = len;
};

Vector3d.prototype.dot = function (v) {
with (this) return x * v.x + y * v.y + z * v.z;
};

Vector3d.prototype.cross = function (v) {
with (this) {
var cx = y * v.z - z * v.y;
var cy = z * v.x - x * v.z;
var cz = x * v.y - y * v.x;
return new constructor (cx, cy, cz);
}
};

Vector3d.prototype.getPerspective = function (viewDist) {
if (viewDist == undefined) viewDist = 300;
return viewDist / (this.z + viewDist);
};

Vector3d.prototype.persProject = function (p) {
with (this) {
if (p == undefined) p = getPerspective();
x *= p;
y *= p;
z = 0;
}
};

Vector3d.prototype.persProjectNew = function (p) {
with (this) {
if (p == undefined) p = getPerspective();
return new constructor (p * x, p * y, 0);
}
};

Vector3d.prototype.rotateX = function (angle) {
with (Math) {
var ca = cosD (angle);
var sa = sinD (angle);
}
with (this) {
var tempY = y * ca - z * sa;
var tempZ = y * sa + z * ca;
y = tempY;
z = tempZ;
}
};

Vector3d.prototype.rotateXTrig = function (ca, sa) {
with (this) {
var tempY = y * ca - z * sa;
var tempZ = y * sa + z * ca;
y = tempY;
z = tempZ;
}
};

Vector3d.prototype.rotateY = function (angle) {
with (Math) {
var ca = cosD (angle);
var sa = sinD (angle);
}
with (this) {
var tempX = x * ca + z * sa;
var tempZ = x * -sa + z * ca;
x = tempX;
z = tempZ;
}
};

Vector3d.prototype.rotateYTrig = function (ca, sa) {
with (this) {
var tempX = x * ca + z * sa;
var tempZ = x * -sa + z * ca;
x = tempX;
z = tempZ;
}
};


Vector3d.prototype.rotateZ = function (angle) {
with (Math) {
var ca = cosD (angle);
var sa = sinD (angle);
}
with (this) {
var tempX = x * ca - y * sa;
var tempY = x * sa + y * ca;
x = tempX;
y = tempY;
}
};

Vector3d.prototype.rotateZTrig = function (ca, sa) {
with (this) {
var tempX = x * ca - y * sa;
var tempY = x * sa + y * ca;
x = tempX;
y = tempY;
}
};

Vector3d.prototype.rotateXY = function (a, b) {
with (Math) {
var ca = cosD (a), sa = sinD (a);
var cb = cosD (b), sb = sinD (b);
}
with (this) {
// x-axis rotation
var rz = y * sa + z * ca;
y = y * ca - z * sa;
// y-axis rotation
z = x * -sb + rz * cb;
x = x * cb + rz * sb;
}
};

Vector3d.prototype.rotateXYTrig = function (ca, sa, cb, sb) {
with (this) {
// x-axis rotation
var rz = y * sa + z * ca;
y = y * ca - z * sa;
// y-axis rotation
z = x * -sb + rz * cb;
x = x * cb + rz * sb;
}
};

Vector3d.prototype.rotateXYZ = function (a, b, c) {
with (Math) {
var ca = cosD (a), sa = sinD (a);
var cb = cosD (b), sb = sinD (b);
var cc = cosD (c), sc = sinD (c);
}
with (this) {
// x-axis rotation
var ry = y * ca - z * sa;
var rz = y * sa + z * ca;
// y-axis rotation
var rx = x * cb + rz * sb;
z = x * -sb + rz * cb;
// z-axis rotation
x = rx * cc - ry * sc;
y = rx * sc + ry * cc;
}
};

Vector3d.prototype.rotateXYZTrig = function (ca, sa, cb, sb, cc, sc) {
with (this) {
// x-axis rotation
var ry = y * ca - z * sa;
var rz = y * sa + z * ca;
// y-axis rotation
var rx = x * cb + rz * sb;
z = x * -sb + rz * cb;
// z-axis rotation
x = rx * cc - ry * sc;
y = rx * sc + ry * cc;
}
};

trace (">> Vector3d class loaded");







TextText



UndeadCraft

职务:版主
等级:4
金币:10.0
发贴:1993
#42003/10/31 14:00:44
还是把源文件贴上来吧.
看不出来到底是怎么回事



5D荣誉斑竹

职务:普通成员
等级:2
金币:10.0
发贴:622
#52003/10/31 20:52:54
父类



UndeadCraft

职务:版主
等级:4
金币:10.0
发贴:1993
#62003/11/1 13:05:59
我明白了,谢谢
做了个测试
my = function (a) {
	this.value1 = a;
};
my.prototype.kill = function() {
	for (var i in this) {
		trace(i+":"+this[i]);
	}
};
my.prototype.alive = function(a) {
	_root.test = new this.constructor(a);
};
ko = new my(20);
ko.alive(30);
ko.kill();
test.kill();



amumu

职务:普通成员
等级:1
金币:0.0
发贴:1
#72004/11/7 17:55:40
这是加拿大人 robertpenner 在FLASHMX编程创意与实现中的代码。
其中constructor 是没有公布的一种父类继承
有什么问题可以
amumu@!26.com 联系我,我对他的作品研究的透彻。



智慧当当

职务:普通成员
等级:2
金币:13.0
发贴:412
#82004/11/12 23:22:24
没错,是加拿大人写的,的确是一种父类继承,那位仁兄的确见多使广啊。
它是继super()的另一种用法!!!