简体   繁体   English

确定JavaScript中对象的类

[英]Determine class of an object in javascript

What can be the best method to determine the class of an object in JavaScript. 在JavaScript中确定对象类的最佳方法是什么。 If I have to determine class of an object 'o', I can do this... 如果必须确定对象“ o”的类,则可以执行此操作...

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

But when we have our custom classes, this method doesn't help. 但是,当我们有自定义类时,此方法无济于事。

You can't really find the class of a object so easily like in Java. 您真的很难像Java中那样轻易找到对象的类。 This is because Javascript is a prototype-based language, But you still have a few methods of determining the class: 这是因为Javascript是一种基于原型的语言,但是您仍有几种确定类的方法:

  • typeof 类型
  • instanceof 实例
  • obj.constructor obj.constructor
  • proto.isPrototypeOf proto.isPrototypeOf

Example: 例:

function Foo() {}
var foo = new Foo();

typeof Foo; // == "function"
typeof foo; // == "object"

foo instanceof Foo; // == true
foo.constructor; // == Foo

Foo.prototype.isPrototypeOf(foo); // == true

Two Objects belongs to the same class if they both inherit from the same prototype object. 如果两个对象都从同一原型对象继承,则它们属于同一类。

When dealing with custom classes and in order to make effective method for class checks (as suggested above Ivan Grgurevic), is very important to explicit set the constuctor and also tell which in on the proptotype. 在处理自定义类时,为了使类检查成为有效的方法(如上文Ivan Grgurevic所建议),显式设置构造函数并在原型中确定哪个是非常重要的。

Example: 例:

// Class constructor
function CustomClass(type) {
  // CODE
}

// prototype, all CustomClass objects inherit form this object
Sedcard.prototype = {
  constructor: CustomClass, // explicit set constructor, backwards reference
  // Class method
};

Now you can safetly use checks such as: typeof, instanceof and direct like Foo.prototype.isPrototypeOf(foo) 现在,您可以安全地使用诸如Foo.prototype.isPrototypeOf(foo)之类的检查:typeof,instanceof和direct

Though, this is a very hard topic on JS and recommend you further lectures, like JS book "Javascript, the definitive guide" http://shop.oreilly.com/product/9780596805531.do in order to deeply understand the way it works. 但是,这是关于JS的一个非常艰巨的主题,建议您进一步讲课,例如JS书“ Javascript,权威指南” http://shop.oreilly.com/product/9780596805531.do ,以便深入了解其工作方式。

A bit more about this topic: 有关此主题的更多信息:

r instanceof CustomClass: The instanceof operator does not actually check whether r was initialized by the constructor. r instanceof CustomClass:instanceof运算符实际上并不检查r是否由构造函数初始化。 It checks whether it inherits from its prototype. 它检查是否从其原型继承。 Nevertheless, the instanceof syntax reinforces the use of constructors as the public identity of a class. 尽管如此,instanceof语法还是加强了使用构造函数作为类的公共标识。

The expression o instanceof c evaluates to true if o inherits from c.prototype. 如果o继承自c.prototype,则表达式o instanceof c的计算结果为true。 The inheritance need not be direct. 继承不必是直接的。 If o inherits from an object that inherits from an object that inherits from c.prototype, the expression will still evaluate to true. 如果o继承自从c.prototype继承的对象继承的对象,则表达式仍将计算为true。

Both quotes comes from the previous book I recommended. 这两个引文均来自我推荐的上一本书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM