數據類型的學習

JS中的數據類型

基本數據類型(值類型)Number,String,Boolean,null ,undefined
引用數據類型object({},[],正則/^$/,日期對象,數學Math,實例對象),function
ES6新增Symbol 唯一值

檢測數據類型 typeof
控制檯輸入 typeof NaN // "number" 不是有效數字,但屬於number數字類型
NaN ,即Not a Number,NaN==NaN不相等:
控制檯輸出如下:

NaN ==0
false
NaN ==1
false
NaN ==NaN
false

檢測是否爲有效數字用isNaN,調用的是*Number()*方法做隱式轉換的。
使用如下:

isNaN('fd')
true
isNaN('')
false
isNaN(' ')
false
isNaN('2rem')
true
Number('2rem')
NaN

把其他數據類型轉化爲Number類型的時候,如果不能轉換就是NaN,比如parseInt/Float, Number() ,數學運算(相加),一些其他比較的時候.

object:
對象的屬性名可以是數字,布爾值,不全是字符串。

let a = {x:100};
let b = function fn(){};
let obj = {0:100,true:'文字'};
obj[a]=1000;
obj[b]=2000;
console.log(obj);
VM1184:1 {0: 100, true: "文字", [object Object]: 1000, function fn(){}: 2000}
obj[0]
100
obj["true"]
"文字"

對象的屬性名一定不能是引用類型的值,默認會把引用類型的值轉化爲字符串進行處理,會將值進行toString();
例子:

let a = {x:100};
let b = {y:300};
let obj={};
obj[a]='hello';
obj[b]='world';
console.log(obj);
{[object Object]: "world"}
console.log(obj[a]===obj[b])
VM2574:1 true

在以上代碼中,引用類型的a和b是作爲屬性名,需要進行toString(),轉化後是:

({x:100}).toString()
"[object Object]"
({y:300}).toString()
"[object Object]"

調用的是object.prototype.toString(),值就是"[object Object]"

數組:
數組是特殊的對象,特殊在於它的屬性名是有規律逐一遞增代表位置的數字索引。

let arr = [2,11,33,4];
console.log(arr)
VM3910:1 (4) [2, 11, 33, 4]0: 21: 112: 333: 4length: 4__proto__: Array(0)

demo:

//第一個
let a = {},
b='0',
c=0;
a[b]='你好';
a[c]='世界';
console.log(a[b])
VM6714:1 世界

//第二個 唯一值
let a = {},b=Symbol('1'),c=Symbol('1');
a[b]='你好';
a[c]='世界';
console.log(a[b]);
VM7053:1 你好

//第三
let a = {},b={n:'1'},c={m:'2'};
a[b]='你好';
a[c]='世界';
console.log(a[b]);
VM7361:1 世界

如何查看對象的方法,控制檯Object.prototype,

Object.prototype
{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
constructor: ƒ Object()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
hasOwnProperty: ƒ hasOwnProperty()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toString: ƒ toString()
valueOf: ƒ valueOf()
toLocaleString: ƒ toLocaleString()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()

或者使用dir(Object);

dir(Object)
VM7735:1 ƒ Object()

console.dir()是查看對象詳細信息
查看Array數組的方法,直接在控制檯輸入Array.prototype

Array.prototype
[constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]length: 0constructor: ƒ Array()concat: ƒ concat()copyWithin: ƒ copyWithin()fill: ƒ fill()find: ƒ find()findIndex: ƒ findIndex()lastIndexOf: ƒ lastIndexOf()pop: ƒ pop()push: ƒ push()reverse: ƒ reverse()shift: ƒ shift()unshift: ƒ unshift()slice: ƒ slice()sort: ƒ sort()splice: ƒ splice()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()entries: ƒ entries()values: ƒ values()forEach: ƒ forEach()filter: ƒ filter()flat: ƒ flat()flatMap: ƒ flatMap()map: ƒ map()every: ƒ every()some: ƒ some()reduce: ƒ reduce()reduceRight: ƒ reduceRight()toLocaleString: ƒ toLocaleString()toString: ƒ toString()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object

或者dir(Array)即可,具體的方法可以參見mdn,地址https://developer.mozilla.org/en-US/docs/Web/JavaScript

一個小題:

let res = parseFloat('left:100px');
if(res===200){
	alert(200)
}else if(res===NaN){
	alert(NaN)
}else if(typeof res ==='number'){
	alert('number') //NaN是number類型
}else{
	alert('Invalid number')
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章