Jsvascript Judging data types

Source: Internet
Author: User

  • Code:
    Function GetType (val) {this.val = Valthis.isint = function (val) {if (typeof (val) = = ' number ') | | (val instanceof number)) {if (/^\d*$/.test (val)) {return true}else{return false}}else{RET Urn false}}this.isfloat = function (val) {if (typeof (val) = = ' number ') | | (val instanceof number))        {if (/^\d*\.\d*$/.test (val)) {return true}else{return false}}else{ return False}}this.isstring = function (val) {if (typeof (val) = = ' string ') | | (Val instanceof String)) {return true}else{return false}}this.isboolean = function (val) {if ((typeof (val) = = ' Boolean ') | | (Val instanceof Boolean))        {return true}else{return false}}this.isarray = function (val) {if (val instanceof Array) { return true}else{return false}}this.isobject = function (val) {if (val instanceof Object && Typeo F val.length = = ' undefined ') {returN true}else{return False}}this.dump = function () {var val = this.val var isint = This.isint (val) var isfloat = This.isfloat (val) var isstring = this.isstring (val) var isbool = This.isboolean (val) var IsArray = This.isarray (val) var isobject = This.isobject (val) if (isint) {return ' int '}else if (isfloat) {Retu RN ' float '}else if (isstring) {return ' string '}else if (isbool) {return ' Boolean '}else if (IsArray)  {return ' array '}else if (isobject) {return ' object '}}}var a = new GetType (1) var aa = A.dump () var B = New GetType (1.1) var bb = B.dump () var c = new GetType (' Hello ') var cc = C.dump () var d = new GetType (true) var dd = D.dump () v Ar e = new GetType ([+]) var ee = e.dump () var f = new GetType ({name: ' Lee '}) var ff = F.dump () console.log (aa) Console.log (BB) Console.log (CC) Console.log (dd) console.log (EE) console.log (FF)
  • Output:
    intfloatstringbooleanarrayobject
  • Jsvascript Judging data types

    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.