Re: [心得] Webgl+Js的Spa

作者: oopFoo (3d)   2019-03-31 22:50:11
之前說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."

Links booklink

Contact Us: admin [ a t ] ucptt.com