Primitives are not objects and don't inherit from anywhere. They can be wrapped in objects which do inherit from Object.prototype
, but these are objects.
It might look like primitives inherit properties, but under the hood these are accessed on the object wrappers, not the primitives. When GetValue resolves a property reference,
So if the base of the reference was a primitive, it will be converted to an object before attempting to get the property.
But as @nnnnnn notes, when you call the method, the this
value will be the primitive, because the base of the reference didn't change. Note in sloppy mode it will be coerced to an object.
In fact, you could take advantage of this and test whether the this
value is an object, if what you want is prevent primitives from being used as bases of references when calling the method:
Object.prototype.method = function() {
"use strict";
if(Object(this) !== this) {
throw Error('This method can only be called on objects');
}
// ...
return "works";
};
true.method(); // Error: This method can only be called on objects
new Boolean(true).method(); // "works"