Preface
This paper mainly explains ES6's expansion of strings, including include, start with and end with, and adds a string template.
Start
Does include () include
What does startsWith() begin with?
What does endsWith() end with?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Character string</title> </head> <body> <script> let str = "Why are you here all the time?"; let res = str.includes('One'); console.log(res); </script> </body> </html>
Returns true
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Character string</title> </head> <body> <script> let str = "Why are you here all the time?"; let res = str.startsWith('you'); console.log(res); </script> </body> </html>
Returns true
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Character string</title> </head> <body> <script> let str = "Why are you here all the time?"; let res = str.startsWith('you'); if(str.startsWith('you')){ Execution statement; }else if(Sentence){ Execution statement; }else{ Execution statement; } </script> </body> </html>
str.endsWith();
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Character string</title> </head> <body> <script> let str = "Why are you here all the time?"; alert( str.endsWith('this')); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Character string</title> </head> <body> <script> let mail = '34234@qq.com'; if( mail.endsWith('@qq.com') || main.endsWith('@163.com') ){ alert('Input correct'); }else{ alert('Please enter your mailbox'); } </script> </body> </html>
String template
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Character string</title> </head> <body> <script> let str2 = ` <ul> <li>content</li> </ul> `; console.log(str2); </script> </body> </html>
Parameters of functions
Function parameters, expansion operator:...
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Function parameter</title> </head> <body> <script> function fun(a,b){ console.log(a,b); } fun(1,2); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Function parameter</title> </head> <body> <script> function fun(a,b,...args){ console.log(a,b); console.log(...args); } fun(1,2,3,4,5); fun(1,2); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Function parameter</title> </head> <body> <script> let arr = [1,2,3]; function fun(a,b,c){ console.log(a); console.log(b); console.log(c); } fun(arr[0],arr[1],arr[2]); fun(...arr); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Function parameter</title> </head> <body> <script> let arr = [1,2,3]; let arr1=[4,5,6]; let array=[...arr,...arr1]; console.log(array); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6-Function parameter</title> </head> <body> <script> function fun(a=1,b=2,c=3){ console.log(a,b,c); } fun(); // fun(4,5,6); </script> </body> </html>
arguments object for understanding functions
arguments is an object, an array of classes
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function add(a,b){ return a+b; } console.log(add(1,2); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function add(){ // Console. log (arguments); // return object return arguments[0] + arguments[1]; } add(1,2); console.log( add(1,2) ); </script> </body> </html>
js object-oriented
// var student = {}; // create objects var student = new Object(); student.name = "dashu"; student.age = 12; student.job = "school student"; student.study = function(){ alert("study"); } student.study();
var student = { name : "dashu", age : 12; job: "school student", study : function(){ alert("study"); } }; student.study();
In data attribute js
var student = { name: "dashucoding" } alert(student.name);
var student={}; Object.defineProperty(student,"name",{ writable:true, value: "dashucoding" }); alert(student.name);
configurable indicates whether attribute values can be deleted by delete, which defaults to true
Enumerable indicates whether the attribute values of an object can be enumerated by for-in, which defaults to true
Wtable indicates whether the attribute value can be modified by default to true
Design Patterns - Singleton Patterns
var mask = function(){ document.body.appendChild(document.createElement('div')); } var obtn = document.getElemetnById("btn"); obtn.onclick = function(){ mask(); }
// Singleton mode var singleton = function(){ var res; return function(fn){ return res||(res=fn.apply(this,arguments)) } }(); var obtn = document.getElementById("btn"); obtn.onclick=function(){ singleton(function(){ return document.body.appendChild(document.createEelement('div')); }) }
Destructuring assignment
Deconstruction assignment is an expression used to obtain data
let arr=[1,2,3]; let a = arr[0]; let b = arr[1]; let c = arr[2]; console.log(a,b,c);
let arr = [1,2,3]; let [a,b,c] = arr; console.log(a,b,c);
let arr = { a:1, b:2, c:3 } let (a,b,c) = arr; // let(a,b,c) = [1,2,3]; console(a,b,c);
epilogue
- This article mainly explains ES6 tutorial - string, function parameters, arguments object, js object-oriented, design pattern - singleton pattern, deconstruction assignment.
- Next, I will continue to explain other knowledge in depth. I am interested in continuing to pay attention to it.
- A little gift or a little compliment