繁体   English   中英

为什么此JavaScript对象方法返回“非函数”方法?

[英]Why is this JavaScript object method returning 'not a function' method?

this.breakintoletters=()=>
        this.lengthi!==0?(this.title2=this.title,this.title2.split(),this.title2.
            map((x)=>this.arol.push(new letter(x))))
                        :!!false 

所以基本上这是代码。 它应该将字符串分解为字母,然后将相关对象推入数组。.检查字符串的长度,如果不为0,则返回MAP函数位于的错误...)TypeError告诉我这不是函数。 编辑器未显示错误。 希望得到帮助

我建议使用不同的方法,事先检查this.lengthi ,然后返回false ,或稍后返回推送值的映射长度。

 this.breakintoletters = () => { if (!this.lengthi) return false; this.title2 = this.title; // this.title2.split(); the result is not taken, not even splitted return this.title2.map(x => this.arol.push(new letter(x))); }; 

您没有将分割后的值分配回this.title2 ,而是在this.split2上使用了map字符串

this.breakintoletters=()=>
        this.lengthi  ? (this.title2=this.title,
                         this.title2=this.title2.split(), 
                         this.title2.map((x)=>this.arol.push(new letter(x))))
                      :false

海事组织,您应该尝试使代码仅在直到它保持可读的程度才有意义,您可以按照以下方式简单地进行处理

this.breakintoletters = ( ) => {
     if(this.lengthi === 0 ) return false;
     this.title2=this.title;
     this.title2=this.title2.split(); 
     return this.title2.map((x) => this.arol.push(new letter(x)))) 
 }

(this.title2 = this.title,this.title2.split(),this.title2。map((x)=> this.arol.push(new letter(x))))

这是this.arol的名称吗?

尝试将其重组为:

(this.title.split()。map((x)=> this.arol.push(新字母(x)))

诸如split(),join(),map()等方法可以链接在一起。

我会重新考虑使用map函数,以及上面其他评论者所涉及的三进制。 我的意思是说它在技术上是可行的,但是如果目标是遍历字符串以推送某些值,那么最好使用for循环。 当您想要迭代以将相同的指定方法应用于每个单个字符时,映射函数更多

同样这只是一种格式化的事情,但是当您在变量和运算符之间留有一些空格并选择对您的工作有意义的变量名称时,它使阅读和理解代码变得容易得多(this.bookLength,this.reverseAr ),或者至少使用通用的this.array或this.arr,这样就更容易提出这样的问题,因为这样您就不会对拼写错误产生清晰的问题,而且如果您打算使用更大的代码库,编写不了解您的人可以理解的简洁代码很重要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM