在three.js继承模式中在原型上使用extend有什么好处? [英] What is the benefit of using extend on the prototype in three.js inheritance pattern?

查看:29
本文介绍了在three.js继承模式中在原型上使用extend有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用优秀的 Three.js 框架,目前正在寻找一个好的 javascript 继承模式,我看看 Three.js 做了什么.我现在对正在发生的事情有了很好的理解,除了一些类",比如 Vector3.

Working with the excellent Three.js framework and currently looking for a good javascript inheritance pattern, I had a look to what is done in Three.js. I now have a good understanding of what's going on, except for some "class" such as Vector3.

特别是,我不清楚为什么有些方法直接添加到原型中,有些方法是在同一个类"中使用 THREE.extend 添加的,如下所示:

Especially, it's not clear for me, why some methods are directly added to the prototype and some are added using THREE.extend in the same "class", like following :

...
THREE.Vector3.prototype = {
    setX: function ( x ) {
         this.x = x;
         return this;

    },
...
};

//and then later in the same file 
THREE.extend( THREE.Vector3.prototype, {
    applyEuler: function () {...}(),
    ...
}

使用扩展有什么好处,而可以增加原型对象?

What's the benefit to use extends, whereas it's possible to augment the prototype object ?

编辑

代码示例是同一个文件的一部分,参见https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js我不是在问这两个部分之间有什么区别,而是为什么在定义原型之后才使用扩展.或者换句话说(使用之前的摘录),为什么不直接写:

The code sample is part of the same file, see https://github.com/mrdoob/three.js/blob/master/src/math/Vector3.js I'm not asking what are the differences between the two parts, but why extend is used just after defining the prototype. Or in other words (with the previous extract), why not just write :

...
THREE.Vector3.prototype = {
    setX: function ( x ) {
         this.x = x;
         return this;

    },
    applyEuler: function () {...}(),
...
};

推荐答案

为什么有些方法直接添加到原型中,有些方法使用THREE.extend添加

Why are some methods directly added to the prototype and some are added using THREE.extend

说真的,这没有任何意义.

Seriously, it doesn't make any sense.

正如您在责备视图中所见,@mrdoob 通过修订版 cc57273 引入了这种奇怪的现象.提交消息说:

As you can find out in the blame view, @mrdoob introduced this oddity with revision cc57273. The commit message says:

恢复到 Object.prototype = { 模式.仅在真正需要它的方法.

Reverting to Object.prototype = { pattern. Only using it on the methods that really need it.

在项目上使用最新版本的lib时神秘莫测我正在处理我收到此错误:

Misteriously when using the latest version of the lib on the project I'm working on I'm getting this error:

> Uncaught TypeError: Object [object Object] has no method 'set'

这是在 THREE.UniformsLib.common.diffuse 初始化一个THREE.Color(THREE.Color.set() 似乎是未定义的).对于一些原因这仅在我在 Three.js 之前加载 Box2D 时发生.如果我加载毕竟是好的.但这也解决了问题.

This is caused when THREE.UniformsLib.common.diffuse initialises a THREE.Color (THREE.Color.set() seems to be undefined then). For some reason this only happens when I load Box2D before three.js. If I load it after is all good. But this fixes the problem too.

这是通过@bhoustonext 恢复提交e2df06e/code> 被引入:

This is reverting commit e2df06e by @bhouston where extend was introduced:

修复三个错过的转换为闭包.切换到扩展数学原型而不是替换它们.这是为了确保类型在类型原型定义中的闭包中创建的使用完整定义更新原型.

fix three missed conversions to closures. switch to extending math prototypes rather than replacing them. This is to ensure that types created in closures within a type's prototype definition get their prototype updated with the full definition.

这篇关于在three.js继承模式中在原型上使用extend有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆