寫下自己的思考是個好習慣,可以總結自己的學習和智慧型,鞏固知識,也可以讓自己不犯同樣錯誤。希望自己可以堅持下來。基本資料型別:number、string、boolean、null、undefined、symbol
複雜資料型別:object 和裡面包含的 function、array、regexp、data
資料型別判斷的方法有很多種,如:typeof
、instanceof
、obejct.prototype.tostring.call()
、constructor
typeof
可以直接返回資料的型別,但是有一些型別是判斷不出來的。
基本型別中,**null ** 型別會判斷成 object
複雜型別中,除了 function 型別,其他一律判斷成 object
const alltype =
, arr:
, func:()
=>
, date:
newdate
, reg:
/regexp/
}object.
keys
(alltype)
.foreach
(key =>)/*
string string
number number
bool boolean
symbol symbol
null_ object
undefined_ undefined
obj object
arr object
func function
date object
reg object
*/
instanceof
可以判斷所有的物件型別
但是判斷不了基本資料型別
a instanceof a
的判斷依據是:a 的原型鏈上面有沒有a.prototype
const string =
"1"const number =
1const bool =
true
const symbol =
symbol
('symbol'
)const null_ =
null
const undefined_ = undefined
const obj =
const arr =
const
func=(
)=>
const date =
newdate
const reg =
/regexp/
console.
log(obj instanceof
object);
// true
console.
log(arr instanceof
array);
// true
console.
log(func instanceof
function);
// true
console.
log(date instanceof
date);
// true
console.
log(reg instanceof
regexp);
// true
這種方法可以判斷所有資料型別
返回形式是:[object 型別名]
const alltype =
, arr:
, func:()
=>
, date:
newdate
, reg:
/regexp/
}object.
keys
(alltype)
.foreach
(key =>)/*
string [object string]
number [object number]
bool [object boolean]
symbol [object symbol]
null_ [object null]
undefined_ [object undefined]
obj [object object]
arr [object array]
func [object function]
date [object date]
reg [object regexp]
*/
直接找變數的原型是**來的,就可以知道這是什麼型別的
返回形式是:[function: 型別]
但是 null 和 undefined 不能通過這種方式判斷
const alltype =
, arr:
, func:()
=>
, date:
newdate
, reg:
/regexp/
}object.
keys
(alltype)
.foreach
(key =>)/*
string [function: string]
number [function: number]
bool [function: boolean]
symbol [function: symbol]
obj [function: object]
arr [function: array]
func [function: function]
date [function: date]
reg [function: regexp]
*/
四種方法中,object.prototype.tostring.call
這個方法個人感覺最好,可以直接判斷所有型別
唯一的缺點是 ie 早期版本用這個方法無法判斷 null 型別
typeof
+instanceof
+===null
也可以判斷所有型別,且網上大多是用這種方法
前端思考日記(2021 1 23)
寫下自己的思考是個好習慣,可以總結自己的學習和智慧型,鞏固知識,也可以讓自己不犯同樣錯誤。希望自己可以堅持下來。this是指向呼叫者的 我們想要改變this指向只要改變函式的呼叫者即可 const newfunc func.bind cur,paras 的效果是把函式的this固定指向cur 也就是...
前端工作思考
2 前端工程的四個階段 3 一般應用確定的過程 產品經理 和 客戶溝通 畫出原型圖 原型圖是用 axure 軟體製作的,匯出來是 html檔案,裡面的都是空白的,軟體生成的css有很多 產品經理將原型圖交給ui畫圖 前端根據ui開發頁面應用,後端根據功能需求開發應用。前端技能體會 前端的知識體系太大...
前端管理日記(1)
第一天入職 1.裝環境 瀏覽器 firefox,chrome,safari,opera,maxthon 工具 ietest,takecolor,sublime,ps,wamp 軟體 pdf,qq,有道筆記本 2.配置郵箱 我的領導是乙個女的,年紀比我稍微年長,今天向她請教管理的心得,她言簡意賅 對手...