2017-09-05 29 views
3

这个项目的目标是重构以前的解决方案来处理实际的对象。目前,当我运行茉莉花测试中,我得到这两个错误:如何获得一个构造函数值来修改其他方法?

类型错误:无法读取未定义

类型错误的特性“分裂”:未定义

无法设置属性“标题”为何类不当我尝试将其传递给其他方法时,识别标题值?在我尝试将值发送到其他方法之前,它似乎工作,但现在我试图将字符串值发送到titleCreator方法,它继续返回undefined。

class bookTitle { 
    constructor(title) { 
     this.title = this.titleCreator(title); // this sets a title value to the bookTitle object/class 
    } 

    titleCreator(string) { 
     // Note that this isn't meant to be a fully fledged title creator, just designed to pass these specific tests 
     var littleWords = ["and", "over", "the"]; // These are the words that we don't want to capitalize 

     var modifiedString = this.string 
     .split(' ') // Splits string into array of words, basically breaks up the sentence 
     .map(function(word,index) { 
      if (index == 0) { 
       return capitalize(word); // capitalize the first word of the string 
      } else if (littleWords.indexOf(word) == -1) { 
       return capitalize(word); // capitalize any words that are not little, the -1 is returned by indexOf if it can't find the word in the array 
      } else if (littleWords.indexOf(word) >= 0) { 
       return word; // do not capitalize as this word is in the list of littleWords 
      } 
     }) 
     .join(' '); // Joins every element of an array into a string with a space inbetween each value. Basically you created a sentence from an array of words 

     return modifiedString; 

    } 

    capitalize(word) { 
     return word.charAt(0).toUpperCase() + word.slice(1); 
     // This function just capitalizes the word given to it 
    } 
} 

module.exports = { 
    bookTitle 
} 

编辑:这里是我的Jasmine测试用例的上下文。该计划的想法只是通过这些案件

var bookTitles = require ('./bookTitles.js'); 

describe('bookTitle', function() { 

    var book; // this is the object that will be passed into the test cases, returns undefined here without beforeEach 

    beforeEach(function() { 
     book = new bookTitles.bookTitle(); // creates a new book instance before each test is run 
    }); 

    describe('title', function() { 
     it('should capitalize the first letter', function() { 
      book.title = 'inferno'; 
      expect(book.title).toEqual('Inferno'); // works without capitalizing 
     }); 

     it('should capitalize every word', function() { 
      book.title = 'stuart little'; 
      expect(book.title).toEqual('Stuart Little'); 
     }); 

     describe('should capitalize every word except...', function() { 
      describe('articles', function() { 
       it('does not capitalize "the"', function() { 
        book.title = 'alexander the great'; 
        expect(book.title).toEqual('Alexander the Great'); 
       }); 

       it('does not capitalize "a"', function() { 
        book.title = 'to kill a mockingbird'; 
        expect(book.title).toEqual('To Kill a Mockingbird'); 
       }); 

       it('does not capitalize "an"', function() { 
        book.title = 'to eat an apple a day'; 
        expect(book.title).toEqual('To Eat an Apple a Day'); 
       }); 
      }); 

      it('conjunctions', function() { 
       book.title = 'war and peace'; 
       expect(book.title).toEqual('War and Peace'); 
      }); 

      it('prepositions', function() { 
       book.title = 'love in the time of cholera'; 
       expect(book.title).toEqual('Love in the Time of Cholera'); 
      }); 
     }); 

     describe('should always capitalize...', function() { 
      it('I', function() { 
       book.title = 'what i wish i knew when i was 20'; 
       expect(book.title).toEqual('What I Wish I Knew When I Was 20'); 
      }); 

      it('the first word', function() { 
       book.title = 'the man in the iron mask'; 
       expect(book.title).toEqual('The Man in the Iron Mask'); 
      }); 
     }); 
    }); 
}); 

回答

0

您试图访问this.string在这行代码:

var modifiedString = this.string 

您设置this.string之前有任何价值。也许你只是想用string,把论点传给titleCreatorthis.stringstring不一样。由于this.string从未被分配,因此它是undefined,因此任何尝试访问它上的方法都将失败。

这一点很难知道你的意图是什么,但也许你的意思是使用使用string代替this.string

titleCreator(string) { 
    // Note that this isn't meant to be a fully fledged title creator, just designed to pass these specific tests 
    var littleWords = ["and", "over", "the"]; // These are the words that we don't want to capitalize 

    var modifiedString = string 
     .split(' ') // Splits string into array of words, basically breaks up the sentence 
     .map(function(word,index) { 
     if (index == 0) { 
      return capitalize(word); // capitalize the first word of the string 
     } else if (littleWords.indexOf(word) == -1) { 
      return capitalize(word); // capitalize any words that are not little, the -1 is returned by indexOf if it can't find the word in the array 
     } else if (littleWords.indexOf(word) >= 0) { 
      return word; // do not capitalize as this word is in the list of littleWords 
     } 
    }) 
    .join(' '); // Joins every element of an array into a string with a space inbetween each value. Basically you created a sentence from an array of words 

    return modifiedString; 
} 

从你的错误的描述,听起来也像你可能有一个问题与你怎么称呼bookTitle(它应该被称为构造在

let bk = new (yourModule.bookTitle)("some string") 

如果你想与帮助,请显示调用构造函数,所以我们可以在太通知调用代码。


这里的代码工作一块,我不得不修复在其他几件事情:

class bookTitle { 
 
     constructor(title) { 
 
      this.title = this.titleCreator(title); // this sets a title value to the bookTitle object/class 
 
     } 
 
    
 
     titleCreator(string) { 
 
      // Note that this isn't meant to be a fully fledged title creator, just designed to pass these specific tests 
 
      var littleWords = ["and", "over", "the"]; // These are the words that we don't want to capitalize 
 
      
 
      var self = this; 
 
    
 
      var modifiedString = string 
 
      .split(' ') // Splits string into array of words, basically breaks up the sentence 
 
      .map(function(word,index) { 
 
       if (index == 0) { 
 
        return self.capitalize(word); // capitalize the first word of the string 
 
       } else if (littleWords.indexOf(word) == -1) { 
 
        return self.capitalize(word); // capitalize any words that are not little, the -1 is returned by indexOf if it can't find the word in the array 
 
       } else if (littleWords.indexOf(word) >= 0) { 
 
        return word; // do not capitalize as this word is in the list of littleWords 
 
       } 
 
      }) 
 
      .join(' '); // Joins every element of an array into a string with a space inbetween each value. Basically you created a sentence from an array of words 
 
    
 
      return modifiedString; 
 
    
 
     } 
 
    
 
     capitalize(word) { 
 
      return word.charAt(0).toUpperCase() + word.slice(1); 
 
      // This function just capitalizes the word given to it 
 
     } 
 
    } 
 
    
 
    let bookTitles = { 
 
     bookTitle: bookTitle 
 
    }; 
 
    
 
    let book = new bookTitles.bookTitle("some title of the book"); 
 
    console.log(book)

事情我不得不修复:

  1. 变化this.string.split(...)string.split(...)
  2. 定义selfthis
  3. 使用self.capitalize()而不是capitalize()来调用构造函数时(你的代码是调用构造函数不带参数,这使得在构造函数中的错误)调用方法正确(在两个地方)
  4. 传递一个字符串。您的代码需要将字符串传递给构造函数。

此外,我们发现您的代码认为只是分配给.title属性会以某种方式运行titleCreator()方法和作出适当的资本。它不会。分配给.title属性只是设置该属性。它不运行任何你的方法。您可以定义一个setter方法,以便在分配给属性时运行代码,但创建一个可以执行所需操作的方法(调用.titleCreator()并将结果分配给.title)可能更有意义。

+0

我以前就这么试过,而且得到了相同的错误代码。我将编辑我的原始文章以包含测试用例,我对缺乏上下文感到抱歉。这个想法只是通过我写的所有茉莉花测试案例 – mcrav95

+0

@ mcrav95 - 还有其他几个问题。请查看我添加到我的答案中的内容,该答案位于可运行代码段中,以便您可以看到它创建的输出并列出我必须做出的更改。 – jfriend00

+0

哇谢谢你,真的超越我的意愿来帮助我。我会审查答案,看看还有什么其他的错误,再次感谢! – mcrav95

相关问题