首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript: Object.setup()不是函数

Javascript: Object.setup()不是函数
EN

Stack Overflow用户
提问于 2018-09-29 04:05:13
回答 2查看 59关注 0票数 0

我有一个重影对象的数组重影,设置是一个重影函数。Javascript不这么认为...

我得到这样的错误:未捕获TypeError: ghosts(ghosts.length - 1).setup不是一个函数

currentWords是一个由字符串数组和设置组成的数组,新的Ghost处于一个区间内。

var Ghost=function()
        {
            this.ghostPic='<img src="ghost.png">';
            this.x=Math.floor(Math.random()*500);
            this.y=Math.floor(Math.random()*500);
            this.speed=5;
            this.width=230;
            this.height=180;
            this.firstH=2000;
            this.dead = false;
            this.word = currentWords[Math.floor(Math.random()*currentWords.length)];
        }
Ghost.prototype.setup=function()
        {
            this.ghostElement=$(this.ghostPic);
            console.log('1');
            this.ghostElement.css({
                height:this.height-=2000,
                width:this.width-=2000,
                position:"absolute",
                zIndex:-1,
                left:this.x,
                top:this.y
            });
            console.log("2");
            $('body').append(this.ghostElement);
        }

然后我像这样调用setup

ghosts.concat(new Ghost());
ghosts[ghosts.length-1].setup();

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-29 04:12:40

您可以选择

ghosts.push(new Ghost());

ghosts = ghosts.concat(new Ghost());

因为Array.prototype.concat返回一个新数组,但Array.prototype.push更改了该数组。

另外,别忘了初始化ghosts数组(在使用push或concat之前),如下所示;

var ghosts = [];
票数 0
EN

Stack Overflow用户

发布于 2018-09-29 04:06:55

.concat合并数组,并且不修改现有数组。也许你想要ghosts.push

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52562024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档