JS的ES5

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

猜你喜欢

转载自www.cnblogs.com/zhihaospace/p/12008083.html
ES5