JS--Day14 (regular related attribute method + ES6 new addition)

1. Compulsory selection of the comprehensive form

var oSexs = document.getElementsByName("sex");
    var oHobby = document.getElementsByName("a");

    var flagSex = false;
    var flagHobby = false;

    function isSelectSex(){
        for(var i=0; i<oSexs.length; i++){
            if(oSexs[i].checked){
                return true;
            }
        }    

        return false;
    }

    function isSelectHobby(){
        for(var i=0; i<oHobby.length; i++){
            if(oHobby[i].checked){
                return true;
            }
        }    

        return false;
    }

    xxx.onsbumit = function(){
        flagSex = isSelectSex();
        flagHobby = isSelectHobby();

        if(flagXXX && flagXXX && flagSex){

        }
    }

2. Regular attributes and methods

①test() Checks whether a string conforms to a certain regular specification, and returns true if it conforms, otherwise returns false

Usage: reg.test(str)

②exec searches according to the regular expression, if the result is satisfied, it will return an array with a length of 1 (the array has only one value)

Usage: reg.exec(str)

var reg = /\d+/g;

var str = "123a456b789";

var arr = reg.exec(str);

③ How to match the following string modifiers that meet the conditions: g global

console.log(arr[0]);

arr = reg.exec(str);console.log(arr[0]);

④search method (regular object as a string parameter)

Returns the position of the first substring that matches the regular expression search Usage: str.search(reg)

⑤ match method

Performs a lookup on a string using a regular expression pattern and returns the results containing the lookup as an array.

     str.match(rgExp)
     var reg = /\d+/g;
     var str = "123a456b789";

     var arr = str.match(reg);
     console.log(arr);

⑥The replace method returns a copy of the string after text replacement according to the regular expression.

stringObj.replace(parameter 1, parameter 2): replace parameter 1 with parameter 2

    var str = "liji de ge bi shi liji";
    str = str.replace(/liji/g,"laowang");
    console.log(str);

three.let

let a = 123;

console.log(a);

① All variables must be defined before use

②The value of the variable cannot be defined repeatedly

③Block-level scope, during each cycle, i is a unique space and will not be destroyed

 let oLis = document.querySelectorAll("li");
     for(let i=0; i<oLis.length; i++){
         oLis[i].onclick = function(){
             console.log(i);
         }
     }

④Temporary dead zone, when the internal variable has the same name as the external variable, the internal variable shields the external variable

    let a = 123;
    {
        let a = 456;
        console.log(a);
    }
    console.log(a);

Four. const

Requirements: Named constants const: Variables modified by const are called read-only variables

①Modify read-only variables

② must be initialized

③ Must be defined before use

④Cannot be defined repeatedly

⑤ Block-level scope

⑥Temporary dead zone

const modifies the stack space, not the heap space

面试题:
 let,const,var的异同?
   1.都是用来定义变量的
   2.var具备声明提升
   3.const修饰只读变量,并且必须初始化
   4.let和cont    
    必须先定义后使用
    不能重复定义
    块级作用域
    暂时性死区

five.this

built-in object of this function

①Used in conjunction with the event body, it represents the element itself that triggers the event

②Used in conjunction with ordinary functions, it represents the object that calls the function

Six. bind

bind: the function of the function object, which modifies the point of this (usually used to modify the anonymous function)

Parameter: bind (modified this points to)

  var oBox = document.querySelector("div");

    document.onclick = function(){
        this.style.display = "none";
    }.bind(oBox);


 var fun = function(){}
    fun == function(){}
    fun.bind(被修改的this指向)===>
    function(){}.bind()

Seven. Mutual conversion between json object and json string

String is the bridge of all data interaction

① JSON string--->JSON object

let str = '{"name":"老王","age":18}';

let json = JSON.parse(str);

console.log(json);

②JSON object--->JSON string

let str1 = JSON.stringify(json);

console.log(str1);

8.7. for...in and for...of

①for(let index in json object){Usually traverse the json string}

Some containers have no subscript or index

②for(let temp of container (cannot be json)){ Usually used to traverse containers without subscripts (set, map)

Each time temp represents the element itself}

Nine. Arrow functions

①Arrow function: shorthand for anonymous function

    document.onclick = ()=>{
        console.log("想念宣文振的第一天");
    }
    let fun = ()=>{
        console.log("想念宣文振的第一天");
    }
    fun();

②Characteristics of arrow functions

1. When the function has only one parameter, the arrow function can omit the parentheses of the parameter
    let fun = a=>{
        console.log(a);
    }
    fun(123);
2. When the function has only one line of code, the curly braces can be omitted
    let fun1 = a=>{
        console.log(a);
    }
    fun1(456);
3. When the function has only one line of code, it comes with return
    let fun2 = a=>a + 10;
    //let fun2 = function(a){
        //return a + 10
    //}
    console.log(fun2(1111));

10. Destructuring assignment

Destructuring assignment: Parsing structure for assignment, parsing array and json object assignment

① Analyze the structure and assign values

a. array
     let [x,y,z] = [1,2,3];
     console.log(x,y,z);

b.json object: benefits, you can remove redundant prefixes

    let json = {
        "name": "老王",
        "age": 18
    }

    let {
        name,
        age
    } = json;
    console.log(name, age);

②Exchange the values ​​of two variables

    let [x, y] = [1, 2];
    [x, y] = [y, x];
    console.log(x, y);

Guess you like

Origin blog.csdn.net/weixin_72756818/article/details/129636265