Getting the Class of a JavaScript Object
In Java, the getClass() method enables retrieval of the class associated with an object. JavaScript, however, lacks an exact equivalent, primarily due to its prototype-based nature.
Determining Object Type and Classes
To determine the type or class of a JavaScript object, various options exist:
Examples:
function Foo() {} var foo = new Foo(); typeof Foo; // == "function" typeof foo; // == "object" foo instanceof Foo; // == true foo.constructor.name; // == "Foo" Foo.prototype.bar = function (x) {return x+x;}; foo.bar(21); // == 42
Note: Uglify, a code minification tool, may alter non-global class names. To prevent this, use the --mangle false parameter during compilation in Gulp or Grunt.
The above is the detailed content of How Do I Get the Class of a JavaScript Object?. For more information, please follow other related articles on the PHP Chinese website!