之前说typedarray不好用,因为subarray出来的view其实很昂贵,整个内存使用量爆增。但因http://glmatrix.net/ 的library的api需要这样,我又不想重写。
现在有个solution分享一下。我加了类似下面这样的程式。
Object.defineProperties(Vertex.prototype, {
0: { get: function() {return Vertex.position[this.posOffset];},
set: function(value) {Vertex.position[this.posOffset] = value;} },
1: { get: function() {return Vertex.position[this.posOffset+1];},
set: function(value) {Vertex.position[this.posOfset+1] = value;} },
2: { get: function() {return Vertex.position[this.posOffset+2];},
set: function(value) {Vertex.position[this.posOffset+2] = value;} }
});
是的,Javascript可以有数字的member。但只能vertex[0]这样access,不能vertex.1。
glmatrix的api,vec3使用array[0], [1],[2]。所以我就define, 0, 1, 2的member。这样就不用subarray出一个float32array(3)的东东。
这算是duck typing, 比class, inheritance更接近Alan Kay,oop之父,定义的oop
http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en
Dr. Alan Kay在这email里把OOP定义清楚。我们所了解的OOP是从Simula 67来的,因c++而普及,其实并不是Alan Kay的OOP。
他的定义是写在最后
"OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them."