Have a look at the possible duplicate What is the reason to use the 'new' keyword at Derived.prototype = new Base and also at this answer on why there are no "classes" in javascript.
Yes, the behaviour is expected. B.prototype
, the object from which your b
instance inherits, is the function object A
. So, it does inherit its name
property which is the string "A"
.
And, this property is immutable (its property descriptor is {configurable: false, enumerable: false, value: "B", writable: false}
), so when you try to assign a new value to b.name
, this will check for [[CanPut]]
which returns false
- and nothing happens. in strict mode, an Invalid assignment in strict mode
TypeError would be thrown (demo).
You only could overwrite it using Object.defineProperty
.
@Edit: I'm not sure why you want those properties inherited by B
. They are "static attributes" of the A
constructor function and they should stay there. There is currently no way to let a Function inherit from anything else than Function.prototype
, so either you go with copying them or your don't use them. Tell us for what you'd need them, and we can find a solution.
Changing the prototype
property of functions (and objects in general) does not make them happen to inherit from anything else. The prototype
property of functions points to the object from which newly created (constructed) instances will inherit.