I tried adding a function y()
into the object constructor x
using prototype chaining. It results to an unexpected
error:
Unexpected token
{
function x(a, b) {
this.a = a
this.b = b
}
x.prototype.y(){
console.log('hello')
}
I want the function x as:
function x(a, b) {
this.a = a;
this.b = b;
y()
}
You're not assigning
y
to a function. Your syntax is invalid. Instead, use an anonymous function:x.prototype.y = function() {...}
See working example below:
If you wish for the method to be static you can omit the
prototype
: