gpt4 book ai didi

javascript - 我怎样才能得到一个构造函数的值给其他方法修改呢?

转载 作者:搜寻专家 更新时间:2023-11-01 00:05:11 25 4
gpt4 key购买 nike

该项目的目标是重构以前的解决方案以使用实际对象。目前,当我运行 Jasmine 测试时,出现以下两个错误:

TypeError: 无法读取未定义的属性 'split'

类型错误:无法设置未定义的属性“标题”

为什么当我尝试将标题值传递给其他方法时,该类无法识别标题值?在我尝试将值发送到其他方法之前,它似乎可以工作,但现在我尝试将字符串值发送到 titleCreator 方法时,它一直返回未定义。

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');
});
});
});
});

最佳答案

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

var modifiedString = this.string

在将 this.string 设置为具有任何值之前。也许您只想使用 string,即传递给 titleCreator 的参数。 this.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. self 定义为 this
  3. 使用 self.capitalize() 而不是 capitalize() 来正确调用方法(在两个地方)
  4. 调用构造函数时传入一个字符串(您的代码调用构造函数时没有参数,这会在构造函数中出错)。您的代码需要将字符串传递给构造函数。

此外,您的代码似乎认为仅分配给 .title 属性将以某种方式运行 titleCreator() 方法并进行适当的大写。它不会。分配给 .title 属性只是设置该属性。它不会运行您的任何方法。您可以定义一个 setter 方法,使其在您分配给该属性时运行代码,但是创建一个 setTitle() 方法来执行您想要的操作(调用 .titleCreator() 并将结果分配给 .title)。

关于javascript - 我怎样才能得到一个构造函数的值给其他方法修改呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com