I'm looking for a way to create a custom Object()
object. I want a way to check the what a given object is an instance of. I need a way of differentiating the custom object from the native.
function CustomObj (data) {
if (data) return data
return {}
}
CustomObj.prototype = Object.prototype
var custom = new CustomObj()
var content = new CustomObj({'hello', 'world'})
var normal = new Object()
console.log(custom) // => {}
console.log(content) // => {'hello', 'world'}
console.log(custom instanceof CustomObj) // => true (expected: true)
console.log(content instanceof CustomObj) // => true (expected: true)
console.log(custom instanceof Object) // => true (expected: false)
console.log(content instanceof Object) // => true (expected: false)
console.log(normal instanceof CustomObj) // => true (expected: false)
console.log(normal instanceof Object) // => true (expected: true)
I'm assuming that this is because I'm inheriting the prototypes
from Object
. I tried adding a this.name
but it didn't change instanceof
.