"foo" instanceof String //=> false
"foo" instanceof Object //=> false
true instanceof Boolean //=> false
true instanceof Object //=> false
false instanceof Boolean //=> false
false instanceof Object //=> false

// the tests against Object really don't make sense

数组文字和对象文字匹配......

[0,1] instanceof Array //=> true
{0:1} instanceof Object //=> true

为什么不是全部?或者,他们为什么不
那么,它们是什么样的实例呢?

在FF3,IE7,Opera和Chrome中也是如此。所以,至少它是一致的。


错过了一些。

12.21 instanceof Number //=> false
/foo/ instanceof RegExp //=> true
有帮助吗?

解决方案

基元是一种与Javascript中创建的对象不同的类型。来自 Mozilla API文档

var color1 = new String("green");
color1 instanceof String; // returns true
var color2 = "coral";
color2 instanceof String; // returns false (color2 is not a String object)

我找不到用代码构造原始类型的任何方法,也许这是不可能的。这可能是人们使用 typeof“foo”的原因。 ===" string" 而不是 instanceof

记住这样的事情的一个简单方法就是问自己“我想知道什么是理智和容易学习的”?无论答案是什么,Javascript都会做另一件事。

其他提示

我用:

function isString(s) {
    return typeof(s) === 'string' || s instanceof String;
}

因为在JavaScript中字符串可以是文字或对象。

在JavaScript中,一切都是对象(或者至少可以被视为对象),除了基元(布尔值,空值,数字,字符串和值 undefined (以及ES6中的符号)):

console.log(typeof true);           // boolean
console.log(typeof 0);              // number
console.log(typeof "");             // string
console.log(typeof undefined);      // undefined
console.log(typeof null);           // object
console.log(typeof []);             // object
console.log(typeof {});             // object
console.log(typeof function () {}); // function

正如您所见,对象,数组和值 null 都被视为对象( null 是对不存在的对象的引用)。区分函数是因为它们是一种特殊类型的可调用对象。但它们仍然是物体。

另一方面,文字 true 0 "" undefined 不是对象。它们是JavaScript中的原始值。但是,布尔值,数字和字符串也分别具有构造函数 Boolean Number String ,它们包含各自的基元以提供附加功能:

console.log(typeof new Boolean(true)); // object
console.log(typeof new Number(0));     // object
console.log(typeof new String(""));    // object

正如您所看到的,当原始值分别包含在 Boolean Number String 构造函数中时,它们将成为对象。 instanceof 运算符仅适用于对象(这就是它为原始值返回 false 的原因):

console.log(true instanceof Boolean);              // false
console.log(0 instanceof Number);                  // false
console.log("" instanceof String);                 // false
console.log(new Boolean(true) instanceof Boolean); // true
console.log(new Number(0) instanceof Number);      // true
console.log(new String("") instanceof String);     // true

正如您所看到的, typeof instanceof 都不足以测试值是布尔值,数字还是字符串 - typeof 仅适用于原始布尔值,数字和字符串;并且 instanceof 不适用于原始布尔值,数字和字符串。

幸运的是,这个问题有一个简单的解决方案。 toString 的默认实现(即它在 Object.prototype.toString 上原生定义)返回两者的内部 [[Class]] 属性原始值和对象:

function classOf(value) {
    return Object.prototype.toString.call(value);
}

console.log(classOf(true));              // [object Boolean]
console.log(classOf(0));                 // [object Number]
console.log(classOf(""));                // [object String]
console.log(classOf(new Boolean(true))); // [object Boolean]
console.log(classOf(new Number(0)));     // [object Number]
console.log(classOf(new String("")));    // [object String]

值的内部 [[Class]] 属性比值 typeof 更有用。我们可以使用 Object.prototype.toString 来创建我们自己的(更有用的) typeof 运算符版本,如下所示:

function typeOf(value) {
    return Object.prototype.toString.call(value).slice(8, -1);
}

console.log(typeOf(true));              // Boolean
console.log(typeOf(0));                 // Number
console.log(typeOf(""));                // String
console.log(typeOf(new Boolean(true))); // Boolean
console.log(typeOf(new Number(0)));     // Number
console.log(typeOf(new String("")));    // String

希望这篇文章有所帮助。要了解有关基元和包装对象之间差异的更多信息,请阅读以下博文: JavaScript的基本生活 基元

您可以使用构造函数属性:

'foo'.constructor == String // returns true
true.constructor == Boolean // returns true
 typeof(text) === 'string' || text instanceof String; 

你可以使用它,它将适用于两种情况

  1. var text =" foo"; // typeof将起作用

  2. String text = new String(" foo"); // instanceof将起作用

我相信我已经提出了一个可行的解决方案:

Object.getPrototypeOf('test') === String.prototype    //true
Object.getPrototypeOf(1) === String.prototype         //false

这在ECMAScript规范中定义第7.3.19节3 如果Type(O)不是Object,则返回false。

换句话说,如果 Obj instanceof Callable 中的 Obj 不是对象, instanceof 将短路到直接。

https://www.npmjs.com/package/typeof

返回 instanceof (构造函数名称)

的字符串表示形式
function instanceOf(object) {
  var type = typeof object

  if (type === 'undefined') {
    return 'undefined'
  }

  if (object) {
    type = object.constructor.name
  } else if (type === 'object') {
    type = Object.prototype.toString.call(object).slice(8, -1)
  }

  return type.toLowerCase()
}

instanceOf(false)                  // "boolean"
instanceOf(new Promise(() => {}))  // "promise"
instanceOf(null)                   // "null"
instanceOf(undefined)              // "undefined"
instanceOf(1)                      // "number"
instanceOf(() => {})               // "function"
instanceOf([])                     // "array"

对我来说是由

引起的混乱
"str".__proto__ // #1
=> String

所以" str" istanceof String 应返回 true ,因为istanceof的工作原理如下:

"str".__proto__ == String.prototype // #2
=> true

表达式#1 #2 的结果相互冲突,因此应该有一个错误。

#1错误

我发现由 __ proto __ 引起的是非标准属性,因此使用标准属性: Object.getPrototypeOf

Object.getPrototypeOf("str") // #3
=> TypeError: Object.getPrototypeOf called on non-object

现在表达#2 #3

之间没有混淆

或者您可以像这样制作自己的功能:

function isInstanceOf(obj, clazz){
  return (obj instanceof eval("("+clazz+")")) || (typeof obj == clazz.toLowerCase());
};

用法:

isInstanceOf('','String');
isInstanceOf(new String(), 'String');

这些都应该返回true。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top