I create an empty Object in ECMAScript without a prototype and define some properties like this:
var obj = Object.create(null)
obj.name = 'John'
obj.age = 27
Here, obj is not an instance of Object.
obj.name // John
obj.age // 27
obj instanceof Object // false
obj.prototype // undefined
obj.constructor // undefined
Trying to make obj an instance of something using two methods to extend this empty Object.
Using Object.__proto__
does not work.
function EmptyObject() {
this.__proto__ = Object.create(null)
this.foo = function() { // could use defineProperty
return this.bar // obviously does not exist here
}
return this
}
var obj = new EmptyObject()
obj instanceof EmptyObject // false!
Extending EmptyObject using ES6 class extends does not work either.
class SpecialObject extends EmptyObject { ... }
let obj = new SpecialObject()
obj instanceof SpecialObject // also false
I tried faking the instance by giving obj a prototype and constructor name property like an ordinary Object but this does not change the return value of instanceof.
Is it somehow possible to make this type of Object an instance of something? Maybe by wrapping it in a function differently or using ES6 class extends? I do not mind iterating over the Object keys or manually clearing them in the function if you think that is the best method of implementation.
The purpose is to create an Object that is not instance of Object (no prototype, etc) but can be instance of something. Is this possible at all? Maybe in a sneaky hacky way.
var obj = new EmptyObject()
obj instanceof Object // false
obj instanceof EmptyObject // true
Thanks!