2017-09-05 62 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

相關問題