在javascript中如何使用new

发布于 2021-11-29 21:05:12 字数 1438 浏览 975 评论 1

请问我像这样写
var DetectCollision = function () {
                //被检测的物体
                this.detectArray = [];
                this.movingCube = function(){
                    var geometry = new THREE.SphereGeometry(0.5);
                    var cube = new THREE.Mesh(geometry);
                    cube.visible = false;

                    return cube;
                };
            };

            DetectCollision.prototype.removeDetector = function(object){

                var index = this.detectArray.indexOf(object);
                if(index === -1) return;

                this.detectArray.splice(index, 1);

            };
            //在数组中添加元素
            DetectCollision.prototype.addDetector = function(object){
                if(this.detectArray.indexOf(object) !== -1) return;
                this.detectArray.push(object);
                return this;
            };

最后我初始化的时候

var detectCollision = new DetectCollision();
            //console.log(detectCollision);
            collisionArray = new DetectCollision().addDetector(cases1);

却提示我

DetectCollision is not a constructor

请问我该怎么写呢??







如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2021-12-01 10:46:17

运行你的代码没有出现上述问题,不过你是不是把初始化代码放到声明代码前面了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文