js 入門級常見問題

來源:互聯網
上載者:User

標籤:javascrip   windows   sci   布爾   eve   length   env   his   floor   

寫在前面:以下是個人總結的關於js常見的入門級的問題一些總結。

js是有 ECMAScript Dom Bom 三部分組成。

1,undefined,NaN,Null,infinity

1) undefined 是undefined 類型

var a; //聲明變數後不賦值

typeof 類型判斷方法

console.log(typeof(a))  //undefined

2) NaN 是 number 型 表示不是一個數字

var a=123;

var b="abc";

a-b 得到NaN

console.log(typeof(a-b)) number

3) Null 物件導向的概念 表示該對象為空白

4)infinity 是number 類型 表示無窮大 除數為0可得

2,js精度問題

js在運算時會存在精度問題:

1)可以先乘以100 1000 ....先化成整數在100 1000
2)number.toFixed(參數) 設定保留小數位元 1.528.toFixed(2) =1.53

3,Math 對象常用幾個函數

1)天花板函數 ceil Math.ceil(1.23)=2 向上返回最小的整數
2)地板函數 floor Math.floor(1.23)=1 向下返回最小的整數
3)隨機數
Math.random() 返回0-1 的隨機數
Math.floor(Math.random()*10) 返回0-9 的隨機數
4) Math.max() Math.min() 返回最大最小的值
5)Math.abs(x)返回一個絕對值
6)Math.round(x) 四捨五入

4,資料類型轉換

1)隱式轉換 變數在運算過程中發生的類型轉換
!! console.log(!!"abc")
2)顯示(強制)轉換:
轉字串:a,(String)變數 b,變數.toString()
轉數字型:a,Number(變數) b,parseInt(變數) c,parseFloat(變數)
轉布爾型:Boolean(變數)
幾種轉換為false的 undefined NaN Null 0 false ""

5,邏輯運算子的短路操作:當運算元不是bool值時

1)隱式轉換
2)從左往右
3) 哪個運算元可以決定結果,就返回這個原運算元
"100"&&0;//true&&false

6,數組檢測方式

var arr=[]
1)Array.isArray(arr) 有相容性問題
2)arr instanceof Array 推薦使用
3) Object.prototype.toString.Call(arr) 可檢測任意類型

7,函數的arguments 和 es6 rest 用法

1)arguments只在函數內部起作用,並且永遠指向當前函數的調用者傳入的所有參數 類似數組
2)rest參數只能寫在最後,前面用...標識
function foo(a, b, ...rest) {
console.log(‘a = ‘ + a);
console.log(‘b = ‘ + b);
console.log(rest);
}

foo(1, 2, 3, 4, 5);
// 結果:
// a = 1
// b = 2
// Array [ 3, 4, 5 ]

8,函數變數提升:

先掃描整個函數體的語句,把所有申明的變數“提升”到函數頂部
‘use strict‘;
function foo() {
var x = ‘Hello, ‘ + y;
alert(x);
var y = ‘Bob‘;
}
foo();
雖然是strict模式,但語句var x = ‘Hello, ‘ + y;並不報錯,原因是變數y在稍後申明了。
但是alert顯示Hello, undefined,說明變數y的值為undefined。
這正是因為JavaScript引擎自動提升了變數y的聲明,但不會提升變數y的賦值。

............................................................................................
變數提升後代碼:
function foo() {
var y; // 提升變數y的申明
var x = ‘Hello, ‘ + y;
alert(x);
y = ‘Bob‘;
}
注意:函數內變數的怪異聲明模式
function fun(){
num=10 //沒寫var 就相當於全域變數
}
fun()
console.log(num) //10

9,this 指向問題

 1 ‘use strict‘; 2     var obj={ 3         name:"test", 4         printName:function(){ 5             console.log(this.name) 6         } 7     } 8     obj.printName(); //顯示 test  this 指的就是obj 9 10     var obj={11             name:"test",12             printName:function(){13                 function showName (){14                     console.log(this.name)15                 } 16                 return showName()17             }18         }19     obj.printName(); //this 指向 undefined (在非strict 模式下指向Windows)20 21      解決方案: var obj={22             name:"test",23             printName:function(){24                 var self=this //在函數外捕獲this25                 function showName (){26                     console.log(self.name)27                 } 28                 return showName()29             }30         }

 

10,sort 排序的坑

1)Array的sort()方法預設把所有元素先轉換為String再排序,如果直接排序數字你就踩坑了
2)預設根據ASCII碼進行排序
3)sort 是一個高階函數,sort(function(){
//寫具體的實現邏輯
})
// 升序
sort(function(a,b){
return a-b
})
//降序
sort(function(a,b){
return b-a
})

11,擷取樣式

getComputedStyle(el,null).width ie 不支援
document.getElementById("btn").currentStyle.width ie提供的
  function getStyle(tag, attr) {  return tag.currentStyle ? tag.currentStyle[attr] : getComputedStyle(tag, null)[attr];  } 

12,拼接字串的問題

在ie7以下存在效能問題,可用數組替換
ie7以上不存在該問題

13,數組的幾個方法

1)arr.slice(start,end) 拷貝數組中的一段資料,返回拷貝的數組
2)splice(start,length) 返回截取數組的部分元素,修改原數組
splice 的參數超過2個會將剩下的參數添加到被截取的數組位置上
arr.splice(1,2,"a","b")

14,清空數組:

1)arr.length=0
2)arr=[] //推薦使用
3)arr.splice(0,arr.length)


15,避免事件被覆蓋的方法(ie9 以下不支援)

標籤.addEventListener(enventType,fn,flase) false預設冒泡 true 捕獲
function fun(){
alert("你好")
}
eg:btn.addEventListener("click",fun)
移除事件監聽(參數必須一致)
btn.removeEventListener("click",fun)


ie-6-10(enventType 加on)
標籤.attachEvent(enventType,fn)
標籤.detachEvent(enventType,fn)

16,事件冒泡,和事件捕獲

事件冒泡:從裡向外執行,遇到相同的事件及執行
事件捕獲:執行順序與冒泡相反(不推薦使用,因為ie使用attachEvent 沒有第三個參數)

阻止事件冒泡
e.stopPropagation()
ie 中阻止事件傳播 cancelBubble=true

17,事件的對象作用:記錄當前事件觸發時的一些資訊

btn.onclick=function(event){}
event.target真正觸發事件的元素
event.type="click"
event.clinetX/clinetY
ie 低版本不相容
var tar=e.target||e.srcElement

18,Json 串的2方法

1)object-->string  JSON.stringify()
2)  string--> obj  JSON.parse()

 

js 入門級常見問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.