Determining Object Class in JavaScript
Unlike Java's .getClass() method, JavaScript does not have an exact equivalent due to its prototype-based nature. However, depending on the desired functionality, there are various alternatives to achieve similar results.
Options:
Examples:
function Foo() {} var foo = new Foo(); typeof Foo; // "function" typeof foo; // "object" foo instanceof Foo; // true foo.constructor.name; // "Foo" Foo.prototype.isPrototypeOf(foo); // true
Note: When using Uglify for code optimization, global class names may be altered. To prevent this, set the --mangle parameter to false in gulp or grunt.
The above is the detailed content of How to Determine Object Class in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!