JS的ES5
1.嚴格模式:
(1)什么是嚴格模式:
-
在全局或函數的第一條語句定義為: 'use strict'
-
如果瀏覽器不支持, 只解析為一條簡單的語句, 沒有任何副作用
(2)嚴格模式作用:
-
必須用var聲明變量
-
禁止自定義的函數中的this指向window
-
創建eval作用域
-
對象不能有重名的屬性
2.json字符串:
分類:
-
json對象
-
json數組
json對象和數組都可以與js的對象和數組相互轉換。
注:如果格式不是json的字符串要轉為js對象就會出錯
轉換函數:
1. js --> json : JSON.stringify(obj/arr)
2. json --> js:JSON.parse(json)
1 var obj = { 2 name : 'kobe', 3 age : 39 4 }; 5 obj = JSON.stringify(obj); 6 console.log( typeof obj);// string 7 console.log(obj); //{"name":"kobe","age":39} 8 9 obj = JSON.parse(obj); 10 console.log(obj); //Object 11 12 var str = "sdfsd"; 13 str = JSON.parse(str); // 報錯 Uncaught SyntaxError: Unexpected token s in JSON at position 0 14 console.log(str);
3.給Object添加了靜態方法
(1) Object.create(prototype, [descriptors])
作用:用一個對象設置另一個對象的原型,並且添加新的屬性
1 var obj = {name : 'curry', age : 29} 2 var obj1 = {}; 3 4 //讓新的對象obj1的原型設置為obj,並添加新的屬性 5 obj1 = Object.create(obj, { 6 sex : { 7 value : '男', 8 writable : true, 9 configurable: true, 10 enumerable: true 11 } 12 });
對象obj設置對象obj1.__proto__,並在obj1上添加新的屬性sex
1 obj1:Object 2 sex: "女" 3 __proto__: Object 4 age: 29 5 name: "curry" 6 __proto__: Object 7 ......
其中的四個參數為對新添加的屬性進行描述
1 value : 指定值 2 writable : 標識當前屬性值是否是可修改的, 默認為false 3 configurable: 標識當前屬性是否可以被刪除 默認為false 4 enumerable: 標識當前屬性是否能用for in 枚舉 默認為false
1 var obj = {name : 'curry', age : 29} 2 var obj1 = {}; 3 4 //讓新的對象obj1的原型設置為obj,並添加新的屬性sex 5 obj1 = Object.create(obj, { 6 sex : { 7 value : '男', 8 writable : true, 9 configurable: true, 10 enumerable: true 11 } 12 }); 13 14 15 obj1.sex = '女'; 16 console.log(obj1); 17 /* 18 Object 19 sex: "女" 20 __proto__: Object 21 age: 29 22 name: "curry" 23 __proto__: Object 24 ...... 25 */ 26 console.log(obj1.age); //29 27 console.log(obj1.sex); //女 28 delete obj1.sex; 29 console.log(obj1); //刪除了實例對象obj1的屬性sex
補:
- for in可以美劇處對象的屬性,同時也會枚舉出原型上的屬性。
- 使用obj1.hasOwnProperty(i)只獲取自身屬性不枚舉原型上屬性。必須設置 enumerable: true
- 使用delete obj1.sex刪除實例對象自身的屬性。必須設置 configurable: true
1 for(var i in obj1){ 2 3 if(obj1.hasOwnProperty(i)){ 4 console.log(i); //只打印一個sex 5 6 } 7 } 8 9 delete obj1.sex; 10 console.log(obj1); //刪除了實例對象obj1的屬性sex
(2) Object.defineProperties(object, descriptors)
作用:為指定對象添加新的屬性
1 //小技巧:防止循環調用set方法,在全局添加變量sex 2 var sex = '男'; 3 var obj3 = { 4 name: 'mynameobj3', 5 age: 22 6 } 7 8 Object.defineProperties(obj3, { 9 sex: { 10 get: function () { 11 //console.log("get調用"); 12 return sex; 13 }, 14 set: function (value) { 15 //console.log("set調用", value); 16 //this.sex = value; 不能這么寫會造成循環調用 17 sex = value; 18 } 19 20 } 21 }); 22 23 console.log(obj3); 24 /* 25 Object 26 age: 22 27 name: "mynameobj3" 28 sex: (...) 29 get sex: () 30 set sex: (value) 31 __proto__: Object 32 ...... 33 */ 34 obj3.sex = '女'; 35 console.log(obj3.sex); //女
obj3添加了sex屬性並可以設置和獲得
4.對象本身的兩個方法
-
get propertypeName()
-
set propertypeName(value)
1 var sex = 'man'; //利用上一個例子的小技巧 2 var obj1 = { 3 name: 'obj1', 4 age: 22, 5 sex: 'man', 6 get sex(){ 7 return sex; 8 }, 9 set sex(value){ 10 sex = value; 11 } 12 } 13 14 console.log(obj1); 15 console.log(obj1.sex); 16 obj1.sex = 'woman'; 17 console.log(obj1.sex);
5.Array的擴展方法
1 1. Array.prototype.indexOf(value) : 得到值在數組中的第一個下標 2 2. Array.prototype.lastIndexOf(value) : 得到值在數組中的最后一個下標 3 3. Array.prototype.forEach(function(item, index){}) : 遍歷數組 4 4. Array.prototype.map(function(item, index){}) : 遍歷數組返回一個新的數組,返回加工之后的值 5 5. Array.prototype.filter(function(item, index){}) : 遍歷過濾出一個新的子數組, 返回條件為true的值
1 var arr = [1, 4, 6, 2, 5, 6]; 2 console.log(arr.indexOf(6));//2 3 //Array.prototype.lastIndexOf(value) : 得到值在數組中的最后一個下標 4 console.log(arr.lastIndexOf(6));//5 5 6 //Array.prototype.forEach(function(item, index){}) : 遍歷數組 7 arr.forEach(function (item, index) { 8 console.log(item, index); 9 }); 10 11 //Array.prototype.map(function(item, index){}) : 遍歷數組返回一個新的數組,返回加工之后的值 12 var arr1 = arr.map(function (item, index) { 13 return item + 10 14 }); 15 console.log(arr, arr1); 16 17 //Array.prototype.filter(function(item, index){}) : 遍歷過濾出一個新的子數組, 返回條件為true的值 18 var arr2 = arr.filter(function (item, index) { 19 return item > 4 20 }); 21 console.log(arr, arr2);
6.修改this的綁定問題
方式:3種
- fun.call(obj,arg1,arg2,...):立即執行
- fun.apply(obj,[arg1,arg2,...]):立即執行
- fun.bind(obj,arg1,arg2,...)():手動加入小括號執行 【適用於回調函數綁定this】
1 var obj = { 2 name: "obj" 3 }; 4 5 function fun(msg){ 6 console.log(this); 7 console.log(msg); 8 } 9 10 fun(); //this ==> window 11 fun.call(obj,"call"); //this ==> obj 立即執行 12 fun.apply(obj,["apply"]); //this ==> obj 立即執行 13 fun.bind(obj,"bind")(); //this ==> obj 手動執行 14 15 16 //回調函數只能用bind來綁定this 17 setTimeout(function(){ 18 console.log(this); 19 }.bind(obj),1000); //注意是回調函數部分綁定this