Reputation: 75
Why it doesn't work? When I'm trying to call example.example() I'm getting TypeError: example.example is not a function.
var example = class {
constructor(){
this.example = false;
}
id(){
this.example = !this.example;
return this.example;
}
};
Upvotes: -1
Views: 60
Reputation: 1164
You have created class so you need to make an object of this.It should work by calling like this.
var example = class {
constructor(){
this.example = false;
}
id(){
this.example = !this.example;
return this.example;
}
};
console.log((new example()).id());
var obj = new example();
console.log(obj.id());
Upvotes: 1
Reputation: 68443
When I'm trying to call example.example() I'm getting TypeError: example.example is not a function.
example
is a reference to an anonymous class, and its constructor can only be invoked with new
You need to call it as
var a = new example();
a.example; //false
Demo
var example = class {
constructor() {
this.example = false;
}
id() {
this.example = !this.example;
return this.example;
}
};
var a = new example()
console.log(a.example);
Upvotes: 3