我必须创建一个要添加到字符串类的函数。例如,“ hi” .exclamify(3)应返回hi并带有三个!!!在字符串的末尾。我知道我必须使用
String.prototype.exclamify = function(n)
{
// Function definition goes here
console.log(this);
}
"hi".exclamify(2);
[当我将该函数称为“ hi”时。exclamify(2);控制台返回[String:'Hello'],该怎么做才能只获取字符串“ Hello”的值?我使用Object.values(this)返回['H','e','l','l','o']。我想要确切的字符串来实现该功能。
似乎this
的值对您有用,并且包含正确的字符串。这是您的问题的快速尝试:
String.prototype.exclamify=function(n){ return `${this}${'!'.repeat(n)}` }
const testString = "hello";
console.log(testString.exclamify(3));
console.log(testString.exclamify(5));