qidao123.com技术社区-IT企服评测·应用市场

标题: 【js基础笔记] - 包含es6 类的利用 [打印本页]

作者: 嚴華    时间: 3 天前
标题: 【js基础笔记] - 包含es6 类的利用
js基础

js 预解析

js 代码的编译和执行
通俗的说,预解析是在代码执行之前,先对代码进行通读息争释 ,之后执行代码
js变量提升

  1.     // 声明式函数
  2.     var fn = 100;
  3.     function fn() {
  4.       console.log('方法执行')
  5.     }
  6.     console.log(fn);//100
  7.     fn()//fn is not a function
复制代码

  1. //undifined 预解析了title的变量名称,没有赋值
  2. console.log(title)
  3. var title = '苹果';
  4. console.log(title)//苹果
复制代码
赋值式函数 和 声明式函数


上述例子中,预解析只是声明了 func这个变量,没有赋值 所以说func()不是 function 只是声明了一个func变量
第一个 函数 只能在 声明后 执行
第二个 声明式函数 可以在之前执行
DOM相干知识

节点选择器

  1. <div id="hbox">
  2.     强大的内心
  3.     <div id="box"></div>
  4. </div>
复制代码

  1.   box.childNodes //获取所有节点
  2.   box.children 所有元素
复制代码

  1.   box.firstChild //获取所有节点中的第一个 包含 text /n
  2.   box.firstElementChild 所有元素 标签
复制代码

获取属性节点

  1. box.getAttribute('index');
  2. console.log(box.attributes[1]);
复制代码
创建节点

创建之后,直接可以利用 '.'方式更改/添加的属性
  1. const newDiv = document.creatElement('div');
  2. newDiv.className = 'newd';
  3. newDiv.id = 'dd';
  4. newDiv.innerHTML = "玫瑰和王子不属于一个世界";
复制代码
插入节点

  1. <div id="box">
  2.     <div id="box1"></div>
  3. </div>
  4. <script>
  5. box.appendChild(newDiv) //在box下增加一个节点
  6. // insertBefore(要插入的节点,谁的前面)
  7. hbox.insertBefore(newDiv,box);
  8. // 删除节点(节点对象)
  9. box.removeChild(newDiv);
  10. // 删除自己以及后代
  11. box.remove();
  12. </script>
复制代码
更换节点

replaceChild(新的节点,老的节点)
  1. <script>
  2. // 替换节点
  3. const newDiv = document.createElement('div')
  4. newDiv.innerHTML = '替换'
  5. box.replaceChild(newDiv,box1)
  6. </script>
复制代码
克隆节点

cloneNode() 默认不传参 仅克隆当前j节点
  1. <script>
  2. // 克隆节点
  3. // cloneNode(true) 克隆包含后代
  4. const clnode = box.cloneNode(true);
  5. clnode.id = 'cloneId';
  6. document.body.appendChild(clnode);
  7. </script>
复制代码
获取节点属性

节点范例属性名属性值元素节点nodeType1属性节点nodeName2文本节点text3
  1. <script>
  2. const box = document.getElementbyId('box')
  3. console.log(box.nodeType)
  4. </script>
复制代码
获取元素尺寸

offsetWith 和 offsetHeight
获取尺寸:内容+padding+border 宽/高 度
clientWidth 和 clientHeight
获取尺寸:内容+padding 宽/高 度
获取元素偏移量

offsetLeft 和 offsetTop
参考点是定位父级 position:relative;
如果元素没有定位,偏移量相对于body
标准的dom变乱流

捕获:window -> document -> body -> outer
目的:inner
冒泡:outer -> body -> document -> window
默认环境 只在冒泡触发 按照 dom2变乱绑定,并进行设置,才能看到捕获的回调函数被触发
制止变乱传播

  1. // propagation 传播
  2. e.stopPropagation();
复制代码
制止默认行为

  1. e.preventDefault();
复制代码

变乱委托

e.target
   减少多个函数绑定 的 性能损耗
动态添加li也 有变乱处理
  可以根据dom流,将变乱委托给父组件,在父组件中获取到点击元素
详见示例
  1. <ul id="list">
  2.   <li><button type="button"></button></li>
  3. </ul>
复制代码

变乱委托,将变乱绑定在父组件<u id='list'>上
此时打印e.target可以获取到整个父节点和子节点
通过获取 e.target.nodeName 选择要绑定的 子组件 节点范例
也可以根据节点操作方式,对节点操作


正则表达式

js复杂范例


字符中包含 连续字符 abc
  1. const reg = /abc/
  2. //验证data.value是否符合表达式规则
  3. reg.test(data.value)
复制代码

  1. const reg = new RegExp("abc")
复制代码
元字符 - 根本元字符


  1.   const reg = /\d/
  2.   console.log(reg.test("abc"))//false
  3.   console.log(reg.test("123"))//true
复制代码

  1.   const reg = /\D/
  2.   console.log(reg.test("abc"))//true
  3.   console.log(reg.test("123"))//false
复制代码

  1.   const reg = /\s/
  2.   console.log(reg.test("ab c"))//true
  3.   console.log(reg.test("123"))//false
复制代码

  1.   const reg = /\S/
  2.   console.log(reg.test("ab c"))//false
  3.   console.log(reg.test("123"))//true
复制代码

  1.   const reg = /\w/
  2.   const reg2 = /\w\w/ //包含两个
  3.   console.log(reg.test("123ab c"))//true
  4.   console.log(reg.test("&*"))//false
复制代码

  1.   const reg = /\W/
  2.   const reg2 = /\W\W/ //包含两个
  3.   console.log(reg.test("123ab c"))//false
  4.   console.log(reg.test("&*"))//true
复制代码

一个符合条件就可以 如果 exec() 截取 只会截取第一个
  1.   const reg = /./
  2.   console.log(reg.test("\n\n\n"))//false
  3.   console.log(reg.test("&\naac*"))//true
复制代码

  1.   const reg = /\d\.\d/
  2.   console.log(reg.test("1.2"))//true
  3.   console.log(reg.test("1a2"))//false
复制代码
元字符 - 边界符


  1. const reg = /^\d/ //以数字开头
  2. console.log(reg.test('abc'))//false
  3. console.log(reg.test('1abc'))//true
复制代码

  1. const reg = /\d$/ //以数字结尾
  2. console.log(reg.test('abc'))//false
  3. console.log(reg.test('abc33'))//true
复制代码

  1. const reg = /^ae$/
  2. console.log(reg.test('abc'))//false
  3. console.log(reg.test('ae'))//true
复制代码
  1.   const reg = /^a\de$/
  2.   console.log(reg.test('abc'))//false
  3.   console.log(reg.test('a2e'))//true
  4.   console.log(reg.test('a232323e'))//false
复制代码
元字符 - 限定符


  1. const reg = /^a\d*c$/;
  2. console.log(reg.test('a222222c'))//true
  3. console.log(reg.test('a222222'))//f
复制代码

  1. const reg = /\d+/;
  2. console.log(reg.test('a222222c'))//true
  3. console.log(reg.test('a222222'))//f
  4. console.log(reg.exec('ads222222'))//可以取出符合条件的字符
复制代码

  1. const reg = /\d?/;
  2. console.log(reg.test('a222222c'))//true
  3. console.log(reg.test('ac'))//true
复制代码

  1.   const reg = /\d{3}/
  2.   console.log(reg.test('ac'))//false
  3.   console.log(reg.test('a2c'))//false
  4.   console.log(reg.test('a232e'))//true
复制代码

  1.   const reg = /\d{1,3}/
  2.   console.log(reg.test('ac'))//false
  3.   console.log(reg.test('a2c'))//true
  4.   console.log(reg.test('a232e'))//true
  5.   console.log(reg.test('a123456e'))//true
  6.   console.log(reg.exec('a123456e'))//123
复制代码
元字符 - 特别符号


  1.   const reg = /(a){2}/
  2.   console.log(reg.test('aca'))//false
  3.   console.log(reg.test('aac'))//true
复制代码

  1.   const reg = /a|2/ // a或者是2
  2.   console.log(reg.test('aca'))//false
  3.   console.log(reg.test('aac'))//true
复制代码
  1.   const reg = /(abc|234)/ //abc或者是 234
  2.   console.log(reg.test('ab234'))//true
  3.   console.log(reg.test('abc34'))//true
  4.   console.log(reg.test('abc'))//true
复制代码
  1.   const reg = /abc|234/ //ab c或2 34
  2.   console.log(reg.test('ab234'))//true
  3.   console.log(reg.test('abc'))//true
  4.   console.log(reg.test('ab34'))//false
  5.   console.log(reg.test('c23'))//false
  6.   console.log(reg.test('234'))//true
复制代码

[a-zA-Z0-9_] 等价于 \w 字母数字下划线
[0-9] <=> \d
  1.   const reg = /[123sad]/
  2.   console.log(reg.test('abc'))//true
  3.   console.log(reg.test('234'))//true
复制代码
  1.   const reg = /[123sad]{3,5}/
  2.   console.log(reg.test('ba2s'))//true
  3.   console.log(reg.test('ab2s'))//false 要包含在【】中 连续在一起的为true
复制代码

^ 写在外面 以什么为开头 写在[]中 取反
  1.   const reg = /[^abc]/
  2.   console.log(reg.test('wabc'))//true
  3.   console.log(reg.test('abc'))//false 要包含在【】中 连续在一起的为^ 取反
复制代码
正则表达式 - 捕获


  1.   const str = 'who say 2025-05-20'
  2.   const reg = /\d{4}-\d{1,2}-\d{1,2}/
  3.   console.log(reg.test(str))//true
  4.   console.log(reg.exec(str)[0])//2025-05-20
复制代码

g 全局匹配 global
  1.   const str = 'who say 2025-05-20 is my birthday,my birthday in 2025-6-1';
  2.   const reg = /\d{4}-\d{1,2}-\d{1,2}/g
  3.   console.log(reg.test(str))//true
  4.   console.log(reg.exec(str)[0])//2025-05-20
复制代码
const reg = /(\d{4})-(\d{1,2})-(\d{1,2})/g该表达式 加了()会 将符合的团体单独提取
  1.   const str = 'who say 2025-05-20 is my birthday,my birthday in 2025-6-1';
  2.   const reg = /\d{4}-\d{1,2}-\d{1,2}/g
  3.   // const reg = /(\d{4})-(\d{1,2})-(\d{1,2})/g 加了()会 将符合的整体单独提取
  4.   console.log(reg.test(str))//true 全局模式 会记录此次内容
  5.   const res1 = reg.exec(str);//2025-6-1',
  6.   const res2 = reg.exec(str);// null  最上面的console.log 输出一次 调用了reg 匹配 全局g记录了输出的操作,所以 只剩下 第二次的结果
复制代码
i 忽略大小写 - ignore
  1.   const str = 'who say 2025-05-20 is my birthday,my birthday in 2025-6-1';
  2.   const reg = /a-z/i
  3.   console.log(reg.test(str))//true
复制代码

示例
贪婪匹配 匹配 符合条件的最大次数
  1.   const reg = /\d{1,4}/
  2.   console.log(reg.exec('aca124856'))//1248  贪婪匹配 匹配 符合条件的最大次数
复制代码
?非贪婪匹配 匹配 符合条件的最小次数
  1.   const reg = /\d{1,4}?/
  2.   console.log(reg.exec('aca124856'))//1  ?非贪婪匹配 匹配 符合条件的最小次数
复制代码
常见搭配
   *?
+?
??
{n,}? {n,m}?
  场景应用
  1.   const str = `<p class='list'> xxxxx <p/>`
  2.   const reg = /^<p.*>$/
  3.   const reg = /./ //一个符合条件就可以
  4.   console.log(reg.exec(str))//<p class='list'> xxxxx <p/>  贪婪匹配
复制代码
  1.   const str = `<p class='list'> xxxxx <p/>`
  2.   const reg = /^<p.*?>$/
  3.   console.log(reg.exec(str))//<p class='list'> xxxxx <p/> >$存在 正则引擎 必须要找到正确的,所以会匹配到 第二个p 。为了满足 $,.*? 会继续扩展直到能匹配到字符串末尾的 >
复制代码
  1.   const str = `<p class='list'> xxxxx <p/>`
  2.   const reg = /^<p.*?>/
  3.   console.log(reg.exec(str))//<p class='list'> 非贪婪匹配
复制代码
正则与字符串写法

   正则.test(字符串)
正则.exec(字符串)
字符串.replace search match
  1.   const str = 'I will marry with my boy friend,I do not want get marry';
  2.   const reg = /marry/g
  3.   console.log(str.replace(reg, '*'))//I will * with my boy friend,I do not want get marry
复制代码
  1.   const str = '';
  2.   const reg = /marry/g
  3.   console.log(str.search(reg))//有返回 开始下标 没有则 -1
复制代码
  1.   const str = 'I will marry with my boy friend,I do not want get marry';
  2.   const reg = /marry/g
  3.   console.log(str.match(reg))// ['marry', 'marry']
复制代码
call、apply、bind的作用与区别

   call apply 和 bind 都是可以修改 this 指向
  利用区别
速记: call 相称于打电话,拨打号码,要输入多个数字
示例:



call 和 apply 修改 this 指向是 立即执行 所以不必要调用函数,直接会执行被修改的function

apply 第二个参数 是 数组 【】




js 类写法 (es6)

本质是 构造函数 class 写法是es6的语法糖 可以用prototype修改
  1. class Hander {
  2.   constructor(name, adge) {
  3.     this.name = name;
  4.     this.adge = adge;
  5.   }
  6.   introduce() {
  7.     console.log('hello', this.name, this.adge)
  8.   }
  9. }
  10. const obj = new Hander('张三',12)//传递的参数,会被构造函数 construct 接收
  11. obj.introduce();//调用类中的 方法
复制代码
js 构造函数 (es5)

  1. function Hander(name,age){
  2.   this.name = name;
  3.   this.age = age;
  4. }
  5. // 节省空间公用内存,将function写在原型prototype上
  6. Hander.prototype.introduce(){
  7.   console.log('挂在原型上的方法',name,age)
  8. }
  9. const obj = new Hander('百里',400);
  10. obj.introduce();
复制代码
js原型链

   每一个对象上都有一个 __proto__
通过 obj.__proto__ 可以获取到上一级方法的构造方法,逐级向上,形成原型链
obj.__proto__ === 构造函数.prototype
  这俩的区别是 __proto__是一个内置函数。雷同君子协议,以__开头的方法不允许被调用。所以,利用prototype代替
原型链的源头:
可以理解为null 由于 Object.prototype.__proto__ = null
也可以说是 Object.prototype
  js面向对象继续 es5

   分为构造函数继续原型继续
通过 改变this指向(call || apply),实现长途继续
通过 prototype 改变父类构造函数 实现增强 或 复用
  
   call 会立即执行函数 参数 允许 恣意多个
apply 会立即执行函数 参数 允许 两个 第二个是[]
  1. function Person(name,age){
  2.   this.name = name;
  3.   this.age = age;
  4. }
  5. function getGrades(){
  6.   console.log(this.name,this.age,'3年级');
  7. }
  8. // 构造函数继承 继承属性
  9. function Student(name,age){
  10.   // call('this要指向的对象',参数1,...,参数n)
  11.   Person.call(this,name,age);
  12. }
  13. const obj = new Student('1班','35号')
  14. console.log(obj)//成功继承 能看到有name 和 age
复制代码
上述中  Person.call(this,name,age); 修改 父方法 Person的this指向 子方法Student 此时,相称于 子方法,存在 父方法的 this.name = name; this.age = age;


  1. // 对象原型继承 继承方法
  2. Student.prototype = new Person();
  3. // 原型增加
  4. Student.prototype.getName = function () {
  5.   console.log(this.name)
  6. }
复制代码

通过this继续父方法中的 getGrades()方法
  1. // 原型增强1
  2. Student.prototype.getGrades2 = function () {
  3.   this.getGrades();
  4.   console.log('增强了原来的方法')
  5. }
复制代码
创建同名方法,修改this指向,进行覆盖 + 增强
// 注意:在原型继续的时间 要在 new 实例化之前
  1. // 原型增强2
  2. Student.prototype.getGrades = function () {
  3.   Person.prototype.getGrades.call(this)
  4.   // console.log('增强了你的方法',this.name)
  5. }
  6. // 原型覆盖
  7. Student.prototype.getGrades = function () {
  8.   console.log('覆盖了你的方法',this.name)
  9. }
  10. const obj = new Student('1班','35号')
  11. obj.getGrades();//可以看到 getGrades方法被调用 成功继承
复制代码
es6 类继续

通常利用super继续父类的变量 和 方法
在子类中,如果是同名方法,父类同名方法会被覆盖。
也可以通过super继续方法,并提升方法
父类代码示例
  1. //父类 father.js
  2. class father{
  3. construct(gender){
  4.    this.gender = gender
  5. }
  6. getToal(){
  7.         console.log('父类方法')
  8. }
  9. }
复制代码
子类继续示例
子类继续中 super关键字 必须在第一行
  1. //子类 child.js
  2. class child extends father{
  3. construct(gender,name,age){
  4.    super(gender)//继承父类的属性
  5.    this.name = name;
  6.    this.age = age;
  7. }
  8. //如果只写 父类同名方法,父类会被覆盖;
  9. //可以使用 super继承父类方法
  10. getToal(){
  11.         super.getToal(){
  12.                 console.log('我是子类')
  13.         }
  14. }
  15. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4