0
Can you help me
class Add { constructor(...words) { this.words = words; } } print(){ var str = this.words.join("
quot;); var b = "quot;+str+"quot;; console.log(b);// var x = new Add("hehe", "hoho", "haha", "hihi", "huhu"); var y = new Add("this", "is", "awesome"); var z = new Add("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit"); x.print(); y.print(); z.print();3 Answers
+ 1
Alim Niyazov
You have defined function outside the class. That should be inside the class.
class Add {
constructor(...words) {
this.words = words;
}
print(){
var str = this.words.join("quot;);
var b = "quot;+str+"quot;;
console.log(b);//
}
}
0
I don't know what my error
0
in addition to declaring the print function as standalone one instead of class (prototype) attached, it lacks closing curly bracket ^^
if you want to declare the function outside of class body, you must use the underlying prototype of constructor (public) function:
class Add {
/* ... */
}
Add.prototype.print = function () {
/* ... */
};