I couldn't figure out this, but maybe anyone can help me: When I define a javascript class and try to add a static attribute "name", I can't use "name" afterwards.
var MyClass = function() {
this.name = 'This is an instance of MyClass';
this.anyName = 'This has nothing to do with it';
}
// static attributes
MyClass.name = 'Why is this not possible?';
MyClass.anyName = 'This works fine!';
var a = new MyClass();
console.log(a.name);
console.log(a.anyName);
console.log(MyClass.name);
console.log(MyClass.anyName);
I would expect it to output all 4 strings. But instead it will only output:
This is an instance of MyClass
This has nothing to do with it
This works fine!
It doesn't accept the static attribute "name", but why? Any ideas/hints? Thanks in advance!
The name property of function objects is read-only, the assignment on it will be ignored.