Important features of ES5

Version awareness
  • ECMA Script (developed by the ECMA organization) is the specification, and JavaScript is the implementation.
  • ES5 (2009) 、 ES6 (2015) 、 ES7 (2016)
  • IE browser has some support problems for ES5.

1. Strict mode ('use strict'): Make Javascript run under stricter grammatical conditions

  • Variable: declaration must use var
  • Object: attributes cannot be repeated
  • Function: The custom function this cannot point to the window object, add scope to the eval function

2. json: The syntax for storing and exchanging text information

  • json.stringFy(): javascript object/array to json text.
  • json.parse(): Convert json text to javascript object/array.

Three. Object extension

  • 1.Object.create(prototype, [descriptors]): Create a new object with the specified object as the prototype
  var obj = {
    
    firstName:"张",lastName:'三'};
  var newObj = null;
  // 一:ES5构建对象:Object.create
  newObj = Object.create(obj,{
    
    
    sex:{
    
    
      value:"男",
      writable:true,// 可修改
      configurable:true,// 可删除
      enumerable:true // 可枚举
    }
  });

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-AEmtv36D-1583675311642) (C:\Users\ASUS\Desktop\md notes\Silicon Valley front end\5 standard integration \ES_567\assets\1583551448985.png)]

  • 2.Object.defineProperties(object, descriptors): extend multiple properties for the specified object
var strongObj = {
    
    
    firstName:"张",
    lastName:'三',
    get fullName(){
    
    
      return this.firstName+this.lastName;
    },
    set fullName(name){
    
    
      var arr = name.split("");
      this.firstName = arr[0];
      this.lastName = arr[1];
    }
  };
  
  var obj = {
    
    firstName:"张",lastName:'三'};
  var strongObj1 = {
    
    firstName:"张",lastName:'三'};
  // 二:ES5增强对象:Object.defineProperties(添加存取器/计算属性)
  Object.defineProperties(strongObj1,{
    
    
    fullName:{
    
    
      get:function(){
    
    
        return this.firstName+this.lastName;
      },
      set:function(name){
    
    
        var arr = name.split("");
        this.firstName = arr[0];
        this.lastName = arr[1];
      }
    }
  });

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-B6MWwWkf-1583675311645) (C:\Users\ASUS\Desktop\md notes\Silicon Valley front end\5 standard integration \ES_567\assets\1583551569778.png)]

Four. Array extension

  • 索引:Array.prototype.indexOf/ lastIndexOf (value)
  • 遍历:Array.prototype.forEach(function(item, index){})
  • 映射:Array.prototype.map(function(item, index){})
  • 过滤:Array.prototype.filter(function(item, index){})
  var arr = [6,2,4,6,1,6];
  console.log(arr.indexOf(6),arr.lastIndexOf(6));
  arr.forEach(function(item){console.log(item)});
  console.log(arr.map(function(item){return item+10}));
  console.log(arr.filter(function(item){return item>4}));

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-jE9kUuVI-1583675311646) (C:\Users\ASUS\Desktop\md notes\Silicon Valley front end\5 standard integration \ES_567\assets\1583573247405.png)]

Five. Function extension

  • Function.prototype.bind(obj): bind this in the function to obj, and return the function
  • Distinguish between bind, call and apply: distinguish from whether to execute immediately or pass parameters
function fn(param){
  console.log(this,param);
}
  var obj = {name:"张三"};
  fn("param");
  fn.call(obj,"call_param");
  fn.apply(obj,["apply_param"]);
  fn.bind(obj,"bind_param")();//不会立即执行,常用在回调函数。

Insert picture description here

Guess you like

Origin blog.csdn.net/jw2268136570/article/details/104741491