我认为JavaScript有5种基本类型(null、undefined、boolean、number、string),然后是object (包括数组、函数和自定义伪类对象)。但有点奇怪的是
typeof null是"object",并且没有简单的方法来获取伪经典类(如Person、Author )的对象类名。我想知道是否有更新的操作符或函数可以为原始类型返回可能的小写名称(对于null,"null",而不是"object"),对于自定义的伪经典对象,返回大小写?
如果ECMA-5或更高版本中不存在这样的运算符或函数,那么使用它有意义吗?否则,我们可能需要依赖我们自己的定义或任何框架,但这不会成为不同平台的标准。
发布于 2012-11-02 05:23:54
ECMAScript 5对象有一个称为[[Class]]的内部属性。这是ES5中最接近您的要求的东西。您可以使用Object.prototype.toString访问[[Class]],如下所示:
function getClassOf(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
getClassOf([ ]); // => "Array"
getClassOf(new Date()); // => "Date"
getClassOf(function() { }); // => "Function"
getClassOf(3); // => "Number"
getClassOf(true) // => "Boolean"
getClassOf(document.createElement('div')); // => "HTMLDivElement"
getClassOf(Math); // => "Math"
getClassOf(null); // => "Null"
getClassOf(undefined); // => "Undefined"
getClassOf({ x: 1 }); // => "Object"此行为对于充分识别来自其他帧的对象至关重要。
但是,它不适用于用户定义的构造函数。使用用户定义构造函数创建的对象具有[[Class]] "Object"。
function Foo() { }
var foo = new Foo();
getClassOf(foo); // => "Object"看起来ECMAScript 6可能有能力扩展Object.prototype.toString返回的内容,这样getClassOf(foo)就可以通过@@toStringTag符号进行"Foo"了。
有关即将推出的标准的更多信息,请参阅https://mail.mozilla.org/pipermail/es-discuss/2012-September/025344.html。
您可以创建自己的函数来执行您想要的操作,如下所示:
function getTypeOf(value) {
// Return "null" for null.
if (value === null) return 'null';
// Return primitive types.
var type = typeof value;
if (type != 'object') return type;
// Return [[Class]] if available for objects.
type = Object.prototype.toString.call(value).slice(8, -1);
if (type != 'Object') return type;
// Return "Object" if it wasn't created with another constructor.
var proto = Object.getPrototypeOf(value);
if (proto == Object.prototype)
return 'Object';
// Return the constructor name if constructor hasn't been
// modified on the object.
if (value.constructor && proto === value.constructor.prototype)
return value.constructor.name;
// Return the constructor name if constructor hasn't been
// modified on the prototype.
if (proto.constructor && proto === proto.constructor.prototype)
return proto.constructor.name;
// Return "???" if the type is indeterminable.
return '???';
}示例:
getTypeOf([ ]); // => "Array"
getTypeOf(new Date()); // => "Date"
getTypeOf(function() { }); // => "Function"
getTypeOf(3); // => "number"
getTypeOf(true) // => "boolean"
getTypeOf(document.createElement('div')); // => "HTMLDivElement"
getTypeOf(Math); // => "Math"
getTypeOf(null); // => "null"
getTypeOf(undefined); // => "undefined"
getTypeOf({ x: 1 }); // => "Object"
function Foo() { }
var foo = new Foo();
getTypeOf(foo); // => "Foo"
// If the constructor property is changed, still works.
foo.constructor = function FakeConstructor() { };
getTypeOf(foo); // => "Foo"
// If the constructor property AND the prototype's constructor is
// changed, result is "???".
foo.constructor = function FakeConstructor() { };
Foo.prototype.constructor = function FakeConstructor2() { };
getTypeOf(foo); // => "???"发布于 2012-11-01 11:32:17
表20中的规范section 11.4.3规定了typeof的行为:
表20 - typeof Operator Results
Type of val Result
-----------------------------------------------------------------------------------
Undefined "undefined"
-----------------------------------------------------------------------------------
Null "object"
-----------------------------------------------------------------------------------
Boolean "boolean"
-----------------------------------------------------------------------------------
Number "number"
-----------------------------------------------------------------------------------
String "string"
-----------------------------------------------------------------------------------
Object (native and does not
implement [[Call]]) "object"
-----------------------------------------------------------------------------------
Object (native or host
and does implement [[Call]]) "function"
-----------------------------------------------------------------------------------
Object (host and does not Implementation-defined except may not be
implement [[Call]]) "undefined", "boolean", "number", or "string".(大写的Null是唯一值为null的内部类型。)
这似乎只是一个约定;null instanceof Object是false,因此,正如预期的那样,null显然不是一个对象。
您请求的操作员不存在。要确定名称,可以使用=== null测试null,使用typeof测试其他原语。至于“自定义伪经典对象”,您可以使用的工具有:
instanceoffunction.prototype,prototype.isPrototypeOfobject.constructor(列表来自this question,您可以在其中看到一些示例。)
发布于 2012-11-01 10:41:17
您现在就可以在所有浏览器中执行此操作。
任何对象的constructor属性都将返回拥有该对象原型的函数。
function Person() { }
alert(new Person().constructor === Person) //truehttps://stackoverflow.com/questions/13170191
复制相似问题