js基礎知識
判斷語句
三個判斷:
if else
三元運算符
switch case
if else
如果括號中只有一個值,默認轉布爾值
如果在對象中,屬性名不存在,那麼獲取的屬性值永遠是undefined
if(3){
console.log(100);
}else{
console.log(200);
}
//3-->true 100
let obj = {name:'sx'}
if(obj.sex){
console.log(100);
}else{
console.log(200)
}
//obj.sex-->undefined undefined-->false 200
括號裏是 兩個值作 == 判斷
if( ({}) == [] ){
console.log(100)
}else{
console.log(200)
}
//對象與對象做==比較,空間不同 false 200
if(1){
//1成立執行的代碼,如果成立,下面都不執行
}else if(2){
//2成立執行的代碼,如果成立,下面都不執行
}else if(3){
//3成立執行的代碼,如果成立,下面都不執行
}else{
//都不成立執行的代碼
}
三元運算符(只適合簡單的判斷)
條件?條件成立執行的代碼:不成立執行的代碼
?:必須成對出現
if([] == false){
console.log(100);
}else{
console.log(200);
}
[] == false?console.log(100):console.log(200);
//三元運算符嵌套判斷:
[] == false?( ![]==true?console.log(100):console.log(200) ):console.log(200);
switch case
num 和case後面的值會進行絕對比較
switch中,讓小括號中的值和case後面的值進行絕對比較
let num = 100;
switch(num){
case"100":
console.log(100);該條件成立時執行的代碼;
break;
case"100px":
console.log(100);
default:
break;//中斷代碼執行
console.log(500);
}
元素
在html中叫標籤,在js中叫元素;
js中的元素都是對象數據類型的
要想操作誰,就要先獲取誰。
document.getElementById
document: 上下文文本
get: 獲取
Element: 元素
By: 通過
Id: id名
document.getElementsByTagName()
在document文本下通過標籤名獲取元素
document 上下文文本
get 獲取
Element 元素
By 通過
Tag 標籤
Name 名字
let div1 = document.getElementById('box');
div1.style.background="black"
div.onclick = function(){
//這裏面的代碼不運行,當點擊元素的時候,這個函數就會執行;
div1.style.background="black"
}
div.onclick = function(){
if(div1.style.background==='black'){
div1.style.background="red"
}else{
div1.style.background="black"
}
}
循環
三個循環
for 循環
for 循環(四部曲)
初始化一個變量 i=0
判斷條件是否成立
執行循環體(大括號裏面的內容)
執行 i++
let arr = [100,2,5,5,2,4];
//用循環拿到數組的每一項
for(let i=0;i<arr.length;i++){
console.log(i);
}
i++
在自身基礎上+1
先取值,再運算
++i 先運算,再取值
以上運算符號做的運算,結果一定是number類型
i+=1:可以理解爲++i
i-=1:可以理解爲--i
let a = 10;
let b = a++;
console.log(b) // 10
console.log(a) // 11
let b = ++a;
console.log(b) // 11
for(let i=0;i<6;i++){
console.log(100) //輸出6次
}
for(let i=0;i<6;i++){
console.log(100); //輸出一次
break; //終止for循環,終止的是整個for循環
}
for(let i=0;i<6;i++){
console.log(100);
continue; //終止本輪循環,continue下面的代碼不執行;不終止整個循環
console.log(200); //不輸出
}
for in 循環:
let arr = {name:'zhufeng',age:10,4:9,1:3};
for(let key in obj){
console.log(key) //1 4 zhufeng 10
//會把對象中屬性名是數字的先輸出,並且按照從小到大的順序輸出
console.log(obj.[key]) // 'zhufeng' 10
// key是變量,代表的是字符串'name' 第一次循環
// 所以不能用 obj.key方法 也不能['key'],就不是變量而是字符串了。
}
while 循環
當不知道循環多少次時,會使用到while循環;
while 阻塞線程;
let i =3;
while(i<6){
i++;
console.log(100)
}
console.log(i) // 6
邏輯且:&& 邏輯或:||
邏輯且:&&
如果前邊轉布爾是true,就取後邊的,反之,就取前邊的
let num = 1&&2
console.log(num)//2
當&&左右兩邊的語句都爲真,則這個邏輯判斷爲真
當&&左右兩邊的語句有一個爲假或都爲假時,這個邏輯判斷爲假
邏輯或:||
如果前邊轉布爾是false,就取後邊的,反之就取前邊的
let num = 1||2
console.log(num)//1
當||左右兩邊的語句有一個爲真,或都爲真時,這個邏輯判斷爲真
當||左右兩邊的語句都爲假時,這個邏輯判斷爲假
代碼運行機制,堆棧內存解析
打開瀏覽器時
分配出一塊棧內存,爲代碼提供運行環境(棧內存形成之後,會分成兩塊,一塊是值存儲區,一塊是變量存儲區)
再分配出一個主線程,執行代碼
代碼執行時,代碼解析從上到下,依次進行,
第一句代碼進棧,執行完畢後出棧,
第二句才能進棧
創建一個變量,變量放到變量存儲區,
此時,如果值是基本數據類型,就放到值存儲區。
如果值是引用數據類型,就開闢一個堆內存,生成一個16進制地址把對象的鍵值存儲進去,把這個16進制的地址返回到值存儲區,相互關聯,把值賦給變量。
例子1
例子2:
列子3:
數據類型檢測
-
typeof
- 檢測數據類型 特點: - 返回值是一個字符串 - 字符串裏放的是他的數據類型 缺點: - typeof檢測數組、普通對象、null 返回值都是'object',所以typeof檢測數據類型,無法將null、數組、普通對象細分
typeof null 返回值是'object'
typeof [] 返回值是'object'
typeof ({}) 返回值是'object'
console.log(typeof 11)//'number'
console.log(typeof '121')//'string'
console.log(typeof undefined)//'undefined'
console.log(typeof true)//'boolean'
console.log(typeof null)//'object'
console.log(typeof [])//'object'
console.log(typeof ({}))//'object'
console.log(typeof typeof typeof 12)//'string'
instanceof
檢測當前實例是否屬於某個類
- 它是檢測當前實例是否屬於某個類,屬於爲true,不屬於爲false
- 寫法:實例instanceof類
- 侷限性:instanceof不能檢測基本數據類型,只能檢測引用數據類型的值 檢測基本數據類型檢測不了只能爲false
constructor
基於構造函數檢測數據類型
- constructor 檢測類 有爲true 沒有爲false
應用方法:
- 1.不能把類的原型重定向
- 2.不能再私有屬性上加constructor
Object.prototype.toString.call()
檢測數據類型最好的方式