【发布时间】:2018-09-02 04:49:33
【问题描述】:
我正在寻找一种方法来确定给定变量是否是对象文字而不是其他任何东西。在下面的示例中,我只希望 a 返回 true 和任何其他给定对象。
var a = {};
var b = [];
var c = new Set();
console.log('typeof:');
console.log(' Literal: ', typeof a === 'object' );
console.log(' Array: ', typeof b === 'object' );
console.log(' Array: ', typeof b === 'object' && !b.prototype );
console.log(' Array: ', typeof b === 'object' && b instanceof Object );
console.log(' Array: ', typeof b === 'object' && !b instanceof Array );
console.log(' Set: ', typeof c === 'object' );
console.log(' Set: ', typeof c === 'object' && !c.prototype );
console.log(' Set: ', typeof c === 'object' && c instanceof Object );
console.log(' Set: ', typeof c === 'object' && !c instanceof Set );
instanceof 如果使用硬编码类进行检查,似乎可以工作,但这对于我正在尝试做的事情是不可行的。
【问题讨论】:
标签: javascript