我创建了一个JavaScript对象,但是我该如何确定那个对象的类?
我想要类似于Java中 .getClass()
方法的功能。
我创建了一个JavaScript对象,但是我该如何确定那个对象的类?
我想要类似于Java中 .getClass()
方法的功能。
在JavaScript中,没有与Java的getClass()
完全对应的方法。这主要是因为JavaScript是一种基于原型的语言,而Java则是一种基于类的语言。
根据您需要使用getClass()
的情况,在JavaScript中有几个选项:
typeof
instanceof
obj.
constructor
func.
prototype
,proto
.isPrototypeOf
以下是一些示例:
function Foo() {}
var foo = new Foo();
typeof Foo; // == "function"
typeof foo; // == "object"
foo instanceof Foo; // == true
foo.constructor.name; // == "Foo"
Foo.name // == "Foo"
Foo.prototype.isPrototypeOf(foo); // == true
Foo.prototype.bar = function (x) {return x+x;};
foo.bar(21); // == 42
--mangle
参数,如果使用gulp或grunt,则可以将其设置为false。func.prototype
(是的,函数是对象,但 prototype
属性只在函数对象上有意义)。 - Milesinstanceof
/isPrototypeOf()
和非标准的 __proto__
。 - ChristophObject.getPrototypeOf()
方法。 - Christophconstructor.name
。函数名称将会任意更改,因此可能无法准确获取。 - igorsantos07construction.name
是一个需要忽略/不压缩的标记。此外,大多数(如果不是全部)的压缩软件都提供了例外规则。 - vulcan ravenobj.constructor.name
在现代浏览器中,Function.name
是一种可靠的方法。它在ES6标准中正式添加,可以将JavaScript对象的“类”作为字符串返回,符合标准。如果使用var obj = new MyClass()
实例化对象,则会返回“MyClass”。
对于数字,它会返回“Number”,对于数组,它会返回“Array”,对于函数等等,它通常表现如预期。唯一失败的情况是如果通过Object.create( null )
创建了一个没有原型的对象,或者对象是从匿名定义(无名称)的函数实例化的。
还要注意,如果你正在压缩代码,直接与硬编码的类型字符串进行比较是不安全的。例如,不要检查obj.constructor.name == "MyType"
,而是检查obj.constructor.name == MyType.name
。或者只比较构造函数本身,但这在DOM边界上不起作用,因为每个DOM上都有不同的构造函数实例。
Function.name
目前还不是 JavaScript 标准的一部分。它目前在 Chrome 和 Firefox 中得到支持,但在 IE(10)中不受支持。 - HalcyonObject.create(something).constructor === something.constructor
这个说法并不完全正确。因此,无论是否使用原型,对于所有使用 Object.create 创建的对象,obj.constructor 都是不可靠的。 - user2451227constructor.name
。函数名称将会任意更改,因此请注意。 - igorsantos07class A { }; class B extends A { }; b = new B(); b.constructor.name; "B"
- Wojciech Kałuskiconstructor.name
与ES6中的新类支持一样所表现出的预期行为是很好的。 - devios1这个getNativeClass()函数会对未定义的值返回"undefined"
,对空值返回"null"
。
对于所有其他值,CLASSNAME
部分是从[object CLASSNAME]
中提取的,这是使用Object.prototype.toString.call(value)
的结果。
getAnyClass()
与getNativeClass()的行为相同,但还支持自定义构造函数。
function getNativeClass(obj) {
if (typeof obj === "undefined") return "undefined";
if (obj === null) return "null";
return Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1];
}
function getAnyClass(obj) {
if (typeof obj === "undefined") return "undefined";
if (obj === null) return "null";
return obj.constructor.name;
}
getClass("") === "String";
getClass(true) === "Boolean";
getClass(0) === "Number";
getClass([]) === "Array";
getClass({}) === "Object";
getClass(null) === "null";
getAnyClass(new (function Foo(){})) === "Foo";
getAnyClass(new class Foo{}) === "Foo";
// etc...
return obj.constructor.name
。这样可以得到相同的结果,还可以处理非本地对象。 - Steve Bennettclass Person {
type = "developer";
}
let p = new Person();
p.constructor.name // Person
n
。
对于这种事情,JS真的很糟糕。 - Askdesigners要获取“伪类(pseudo class)”,你可以通过以下方式获取构造函数:
obj.constructor
假设您在继承过程中已正确设置了constructor
,方式类似于:
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
而这两行与以下代码一起:
var woofie = new Dog()
将使woofie.constructor
指向Dog
。请注意,Dog
是一个构造函数,也是一个Function
对象。但您可以使用if (woofie.constructor === Dog) { ... }
。
如果您想将类名作为字符串获取,我发现以下内容非常有效:
http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects
function getObjectClass(obj) {
if (obj && obj.constructor && obj.constructor.toString) {
var arr = obj.constructor.toString().match(
/function\s*(\w+)/);
if (arr && arr.length == 2) {
return arr[1];
}
}
return undefined;
}
代码到了构造函数,将其转换为字符串并提取构造函数的名称。
请注意obj.constructor.name
可能效果很好,但它不是标准。它在Chrome和Firefox上可用,但在IE上不可用,包括IE 9或IE 10 RTM。
通过使用constructor 属性,您可以获取创建对象的构造函数引用:
function MyObject(){
}
var obj = new MyObject();
obj.constructor; // MyObject
如果你需要在运行时确认一个对象的类型,你可以使用instanceof操作符:
obj instanceof MyObject // true
我曾经遇到了需要使用通用方法的情况,然后我使用了这个:
class Test {
// your class definition
}
nameByType = function(type){
return type.prototype["constructor"]["name"];
};
console.log(nameByType(Test));
这是我找到的获取输入类型类名的唯一方法,如果你没有对象实例。
(使用ES2017编写)
点表示法也可以正常工作。
console.log(Test.prototype.constructor.name); // returns "Test"
class Foo {
get foo () {
console.info(this.constructor, this.constructor.name)
return 'foo'
}
}
class Bar extends Foo {
get foo () {
console.info('[THIS]', this.constructor, this.constructor.name, Object.getOwnPropertyNames(this.constructor.prototype))
console.info('[SUPER]', super.constructor, super.constructor.name, Object.getOwnPropertyNames(super.constructor.prototype))
return `${super.foo} + bar`
}
}
const bar = new Bar()
console.dir(bar.foo)
babel-node
输出的结果:> $ babel-node ./foo.js ⬡ 6.2.0 [±master ●]
[THIS] [Function: Bar] 'Bar' [ 'constructor', 'foo' ]
[SUPER] [Function: Foo] 'Foo' [ 'constructor', 'foo' ]
[Function: Bar] 'Bar'
'foo + bar'
就是这样!在2016年,JavaScript中有一个class
关键词,但仍然没有类类型。this.constructor
是获取构造函数的最佳方式,this.constructor.prototype
是访问原型本身的最佳方式。
在ES6中,您可以使用object.constructor
来处理Javascript类。在下面的示例类中,getClass()
方法返回预期的ES6类:
var Cat = class {
meow() {
console.log("meow!");
}
getClass() {
return this.constructor;
}
}
var fluffy = new Cat();
...
var AlsoCat = fluffy.getClass();
var ruffles = new AlsoCat();
ruffles.meow(); // "meow!"
getClass
方法实例化类,请确保将其包装在括号中,例如:ruffles = new ( fluffy.getClass() )( args... );
。该代码涉及IT技术方面的内容。不要使用o.constructor
,因为它可能会被对象内容更改。相反,使用Object.getPrototypeOf()?.constructor
。
const fakedArray = JSON.parse('{ "constructor": { "name": "Array" } }');
// returns 'Array', which is faked.
fakedArray.constructor.name;
// returns 'Object' as expected
Object.getPrototypeOf(fakedArray)?.constructor?.name;