摘要:類型的實(shí)例首先要理解的含義是例子的意思,實(shí)際上是判斷是否是的一個(gè)實(shí)例。
數(shù)據(jù)類型深入理解 數(shù)據(jù)類型分類 基本(值)類型(5種)
String:任意字符串
Number:任意的數(shù)字
boolean:true/false
null:null
undefined:undefined
對(duì)象(引用)類型(3種)Object:任意對(duì)象
Array:一種特別的對(duì)象(數(shù)值下標(biāo),內(nèi)部數(shù)據(jù)是有序的)
Function:一種特別的對(duì)象(可以執(zhí)行)
數(shù)據(jù)類型判斷(3種方式) typeof :返回?cái)?shù)據(jù)類型的字符串表達(dá)var a console.log(a) // undefined console.log(typeof a) // "undefined" console.log(a === undefined) // true console.log(typeof a === undefined) // false console.log(typeof a === "undefined") // true console.log(undefined === "undefined") // false a = 4 console.log(typeof a) // "number" console.log(typeof a === Number) // false console.log(typeof a === "number") // true a = "hahha" console.log(typeof a) // "string" a = false console.log(typeof a) // "boolean" a = null console.log(typeof a) // object console.log(a === null) // true
注意:typeof返回的是數(shù)據(jù)類型的字符串表達(dá)形式。
typeof true //"boolean" typeof "hahha" //"string" typeof 12 //"number" typeof null //"object" typeof ccc //"undefined" typeof function(){} //"function" typeof {} //"object"instanceof:類型的實(shí)例
首先要理解instanceof的含義:
instance 是例子的意思,A instanceof B 實(shí)際上是判斷A是否是B的一個(gè)實(shí)例。理解了這一點(diǎn),就不難判斷類型了。
var b1 = { b2: [1, "hehe", console.log], b3: function () { console.log("b3") return function () { return "Mandy" } } } console.log(b1 instanceof Object) // true console.log(b1.b2 instanceof Array, b1.b2 instanceof Object) // true true console.log(b1.b3 instanceof Function, b1.b3 instanceof Object) //true true console.log(typeof b1.b2) // "object" console.log(typeof b1.b3) // "function" console.log(typeof b1.b2[1]) // "string" console.log(typeof b1.b2[2]) // "function" b1.b2[2](555) // 555 console.log(b1.b3()()) // "b3" "Mandy"
注意:
函數(shù)既是 Function 類型,也是 Object 類型
數(shù)組既是 Array 類型,也是 Object 類型
===可以判斷undefined 和 null
ccc === "undefined" // true null === null // true總結(jié)
typeof :
可以判斷 undefined / 數(shù)值 / 字符串 / 布爾值 / function
不能判斷 null 與 object, array 與 object
typeof null // "object" typeof [] // "object"
instanceof:
判斷對(duì)象的具體類型
A instanceof B
===:
可以判斷 undefined , null
undefined 與 null 的區(qū)別?undefined 代表定義了,未賦值
null 代表定義了,并且賦值了,只是賦的值為 null
// undefined與null的區(qū)別? var a console.log(a) // undefined a = null console.log(a) // null什么時(shí)候給變量賦值為null?
初始賦值,表明將要賦值為對(duì)象。因?yàn)?typeof null === "Object"
結(jié)束前,讓對(duì)象成為垃圾對(duì)象(被垃圾回收器回收)
//起始 var b = null // 初始賦值為null, 表明將要賦值為對(duì)象 //確定對(duì)象就賦值 b = ["atguigu", 12] //最后 b = null // 讓b指向的對(duì)象成為垃圾對(duì)象(被垃圾回收器回收)嚴(yán)格區(qū)別變量類型 與 數(shù)據(jù)類型?
數(shù)據(jù)的類型:
基本類型
對(duì)象類型
變量的類型(變量?jī)?nèi)存值的類型)
基本(值)類型:保存的就是基本類型的數(shù)據(jù)
引用類型:保存的是地址值
理解實(shí)例 與 類型// 實(shí)例: 實(shí)例對(duì)象 // 類型: 類型對(duì)象 function Person (name, age) {// 構(gòu)造函數(shù) 類型 this.name = name this.age = age } var p = new Person("tom", 12) // 根據(jù)類型創(chuàng)建的實(shí)例對(duì)象
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/104058.html
摘要:設(shè)計(jì)模式是以面向?qū)ο缶幊虨榛A(chǔ)的,的面向?qū)ο缶幊毯蛡鹘y(tǒng)的的面向?qū)ο缶幊逃行┎顒e,這讓我一開始接觸的時(shí)候感到十分痛苦,但是這只能靠自己慢慢積累慢慢思考。想繼續(xù)了解設(shè)計(jì)模式必須要先搞懂面向?qū)ο缶幊?,否則只會(huì)讓你自己更痛苦。 JavaScript 中的構(gòu)造函數(shù) 學(xué)習(xí)總結(jié)。知識(shí)只有分享才有存在的意義。 是時(shí)候替換你的 for 循環(huán)大法了~ 《小分享》JavaScript中數(shù)組的那些迭代方法~ ...
摘要:詳解十大常用設(shè)計(jì)模式力薦深度好文深入理解大設(shè)計(jì)模式收集各種疑難雜癥的問題集錦關(guān)于,工作和學(xué)習(xí)過程中遇到過許多問題,也解答過許多別人的問題。介紹了的內(nèi)存管理。 延遲加載 (Lazyload) 三種實(shí)現(xiàn)方式 延遲加載也稱為惰性加載,即在長(zhǎng)網(wǎng)頁中延遲加載圖像。用戶滾動(dòng)到它們之前,視口外的圖像不會(huì)加載。本文詳細(xì)介紹了三種延遲加載的實(shí)現(xiàn)方式。 詳解 Javascript十大常用設(shè)計(jì)模式 力薦~ ...
摘要:的翻譯文檔由的維護(hù)很多人說,阮老師已經(jīng)有一本關(guān)于的書了入門,覺得看看這本書就足夠了。前端的異步解決方案之和異步編程模式在前端開發(fā)過程中,顯得越來越重要。為了讓編程更美好,我們就需要引入來降低異步編程的復(fù)雜性。 JavaScript Promise 迷你書(中文版) 超詳細(xì)介紹promise的gitbook,看完再不會(huì)promise...... 本書的目的是以目前還在制定中的ECMASc...
摘要:引用類型參數(shù)的傳遞與引用類型的復(fù)制一樣,傳遞的是內(nèi)存地址。指向一個(gè)新的地址,與不再指向同一個(gè)地址官方解釋來一發(fā)中所有函數(shù)的參數(shù)都是按值傳遞的。總結(jié)很簡(jiǎn)單,函數(shù)參數(shù)都是按值傳遞都是棧內(nèi)數(shù)據(jù)的拷貝。 基本類型與引用類型 值類型(基本類型):String,Number,Boolean,Null,Undefined。 引用類型:Array、Object、Function、Date等有多個(gè)值...
閱讀 3590·2021-08-31 09:39
閱讀 1885·2019-08-30 13:14
閱讀 2955·2019-08-30 13:02
閱讀 2799·2019-08-29 13:22
閱讀 2375·2019-08-26 13:54
閱讀 801·2019-08-26 13:45
閱讀 1615·2019-08-26 11:00
閱讀 1013·2019-08-26 10:58