In javascript, it seems like if an object inherits from a function, it simply can't use the name property. I've tested this in node.js, and its likely true in various browsers. E.g.:
var A = function() {}
A.prototype = function() {}
var x = new A()
x.name = "bob"
console.log(x.name) // logs blank
It seems almost absurd that you can't override properties given in an objects prototype (ie __proto__) in this case. Am I going crazy?
Well this isn't because of the function but because there is a descriptor
in the prototype for that name with writable false. Object.getOwnPropertyDescriptor(function(){}, "name").writable === false
If you create a described unwritable property normally, the same happens too:
function A() {
}
Object.defineProperty( A.prototype, "name", {
value: ""
});
var a = new A()
a.name
//""
a.name = "bob"
//"bob"
a.name
//""
You need to go through the Object.defineProperty to do it:
Object.defineProperty(x, "name", {value: "bob"})
x.name
//"bob"