What types of recognition are available in JS

Source: Internet
Author: User

  1. 1. Use typeof.
  2. Direct use without encapsulation.
  3. Flag type can be recognized (except NULL)
  4. Specific object types are not recognized
  5. typeof(1); "Number"
  6. typeof(""); "String"
  7. typeof([]); "Object"
  8. typeof({}); "Object"
  9. typeof(null); "Object"
  10. typeof(undefined); "Undefined"
  11. 2.instanceof
  12. No need to use encapsulation
  13. Ability to distinguish built-in object types
  14. [] instanceof Array; True
  15. /\d/ instanceof RegExp; True
  16. Primitive types cannot be distinguished
  17. 1 instanceof number ; False
  18. "Xiaohong" instanceof String; False
  19. Ability to discriminate between custom types
  20. function point (x, y) {
  21. this. x = x;
  22. this. y = y;
  23. }
  24. var c = new Point (2,3);
  25. C instanceof Point ; True
  26. 3.object.prototype.tostring.call () method.
  27. Object.prototype.toString.call (21); //"[Object number]"
  28. Object.prototype.toString.call ([]); //"[Object Array]"
  29. For ease of use, the function is typically encapsulated as follows
  30. function Typeprototype (obj) {
  31. return Object.prototype.toString.call (obj). Slice (8,-1);
  32. }
  33. Typeprototype ("Guo"); "String"
  34. Typeprototype ({}); //"Object"
  35. Can recognize standard types, and built-in object types
  36. Custom type not recognized
  37. 4.constuructor points to the constructor that constructs the object itself:
  38. Can distinguish the original type
  39. "Guo". constructor = = = String; True
  40. (1). constructor = = = number; //true
  41. True. Constructor = = = Boolean; True
  42. ({}). constructor = = = Object; //true
  43. Can discriminate built-in object type
  44. New Date (). constructor = = = Date; True
  45. [].constructor = = = Array; //true
  46. Custom types are recognized
  47. function people (x, y) {
  48. this. x = x;
  49. this. y = y;
  50. }
  51. var c = new people (2,3);
  52. C.constructor===people; //true
  53. The function is encapsulated as follows
  54. function getconstructorname (obj) {
  55. return obj && obj.constructor && obj.constructor.toString (). Match (/function\s* ([^ (] *)/) [1];
  56. }
  57. Using && short-circuit characteristics
  58. 1 obj//can be returned directly if the entry parameter is undefined and null because it has no constructor value
  59. 2 obj.constructor//Ensure that the following expression is executed correctly
  60. 3 obj.constructor.toString ()//convert constructor to string "function number () {}"
  61. 4. Match (/function\s* ([^ (]*)/) [1]//Gets the value of the type in the constructor string "number"
  62. Getconstructorname (new Date ()); "Date"
  63. Getconstructorname (null); Null
  64. Getconstructorname (12); //"number"

What types of recognition are available in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.