创建自定义对象字面量
Creating a custom object literal
我正在寻找一种创建自定义 Object()
对象的方法。我想要一种方法来检查给定对象是什么的实例。我需要一种方法来区分自定义对象和本机对象。
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)
我假设这是因为我从 Object
继承了 prototypes
。我尝试添加一个 this.name
但它没有改变 instanceof
.
我相信这可以满足您的要求。请注意,需要在使用此解决方案的构造函数中定义原型的属性,因为原型将被覆盖。
function CustomObj (data) {
CustomObj.prototype = Object.create(null)
CustomObj.prototype.x = "Hello world!"
return Object.create(CustomObj.prototype)
}
var custom = new CustomObj()
var normal = new Object()
console.log(custom.x); // => "Hello world!" (expected: "Hello world!")
console.log(custom instanceof CustomObj) // => true (expected: true)
console.log(custom instanceof Object) // => false (expected: false)
console.log(normal instanceof CustomObj) // => false (expected: false)
console.log(normal instanceof Object) // => true (expected: true)
This answer is wrong. I'm keeping it here because the comments might be useful to understand the question.
所有表达式的计算结果为真的原因是 instanceof
在左侧的实例原型中搜索右侧构造函数("type")的原型链。 Object
的原型和 CustomObj
的原型是相同的,因此表达式在所有情况下都为真
function Custom (literal) {
if (literal) return literal
return {}
}
Custom.prototype = Object.prototype
Custom.prototype.constructor = Custom
var foo = new Custom()
if (foo.constructor.name === 'Custom') {
// foo is a custom object literal
}
// However it registers as an instance of both `Custom` and `Object`
console.log(foo instanceof Custom) // => true
console.log(foo instanceof Object) // => true
console.log({} instanceof Custom) // => true
console.log({} instanceof Object) // => true
我正在寻找一种创建自定义 Object()
对象的方法。我想要一种方法来检查给定对象是什么的实例。我需要一种方法来区分自定义对象和本机对象。
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)
我假设这是因为我从 Object
继承了 prototypes
。我尝试添加一个 this.name
但它没有改变 instanceof
.
我相信这可以满足您的要求。请注意,需要在使用此解决方案的构造函数中定义原型的属性,因为原型将被覆盖。
function CustomObj (data) {
CustomObj.prototype = Object.create(null)
CustomObj.prototype.x = "Hello world!"
return Object.create(CustomObj.prototype)
}
var custom = new CustomObj()
var normal = new Object()
console.log(custom.x); // => "Hello world!" (expected: "Hello world!")
console.log(custom instanceof CustomObj) // => true (expected: true)
console.log(custom instanceof Object) // => false (expected: false)
console.log(normal instanceof CustomObj) // => false (expected: false)
console.log(normal instanceof Object) // => true (expected: true)
This answer is wrong. I'm keeping it here because the comments might be useful to understand the question.
所有表达式的计算结果为真的原因是 instanceof
在左侧的实例原型中搜索右侧构造函数("type")的原型链。 Object
的原型和 CustomObj
的原型是相同的,因此表达式在所有情况下都为真
function Custom (literal) {
if (literal) return literal
return {}
}
Custom.prototype = Object.prototype
Custom.prototype.constructor = Custom
var foo = new Custom()
if (foo.constructor.name === 'Custom') {
// foo is a custom object literal
}
// However it registers as an instance of both `Custom` and `Object`
console.log(foo instanceof Custom) // => true
console.log(foo instanceof Object) // => true
console.log({} instanceof Custom) // => true
console.log({} instanceof Object) // => true