There are two problems with your approach: 1. You need to maintain the name space to be unique for each class. This does not scale in big projects. 2. You don't handle subclass. For instance, var dog = new Dog(); dog.instanceof(Animal); // true dog.instanceof(Dog); // true To resolve 1, you can simply use the class object itself rather than a name as the unique identifier of the class. To resolve 2, you can define a instanceof function yourself and keep overriding it in subclasses.
【在 c*****n 的大作中提到】 : 如果是检查primitive type, 用typeof, 否则用instanceof。 : 如果是你自己的class/object, 在class当中加上classNamAnnotation property. 比如 : var Animal = function(){ : this.className = 'animal'; : } : var dog = new Animal(); : console.log(dog.className); : 这样做比instanceof 更快。
【在 b***e 的大作中提到】 : There are two problems with your approach: : 1. You need to maintain the name space to be unique for each class. This : does not scale in big projects. : 2. You don't handle subclass. For instance, : var dog = new Dog(); : dog.instanceof(Animal); // true : dog.instanceof(Dog); // true : To resolve 1, you can simply use the class object itself rather than a name : as the unique identifier of the class. : To resolve 2, you can define a instanceof function yourself and keep