使用正則實現 getType方法

王東煜發表於2019-09-25
Object.prototype.getType = function(){
    /*1. let str = this.constructor.toString();//調取到基類上的tostring方法'[object String]'*/
            /*2. let str = Object.prototype.toString.call(this);*///調取到基類上的tostring方法'[object String]'*/
     let str = ({}).toString.call(this);//調取到基類上的tostring方法'[object String]'
     let reg = /[a-z]+/ig;//擷取字串
     /*1. let reg = / (\w+)/;//擷取字串*/
            /*2. str = str.slice(9, str.indexOf('('));//擷取字串
            return str.toLocaleLowerCase();//首字母小寫*/
   reg.test(str);
   return reg.toLowerCase();
}
console.log(''.getType())
console.log([].getType())
複製程式碼

相關文章