博客列表 >构造函数、原型、dom基础

构造函数、原型、dom基础

咸鱼老爷
咸鱼老爷原创
2021年03月02日 16:43:05604浏览

迭代器

实现一个迭代器

  1. // 手写一个迭代器
  2. function myIterator(data) {
  3. // 迭代器中必须要有一个next()
  4. let i = 0;
  5. return {
  6. next() {
  7. // done表示遍历是否完成
  8. // value表示当前遍历的数据
  9. // let done=i>=data.length?false:true;
  10. let done = i >= data.length;
  11. let value = !done ? data[i++] : undefined;
  12. return {
  13. done,
  14. value
  15. };
  16. }
  17. }
  18. }
  19. let interator = myIterator(['a', 'b', 'c']);
  20. console.log(interator.next());
  21. console.log(interator.next());
  22. console.log(interator.next());
  23. console.log(interator.next());

构造函数与原型

  • 任何一个函数都有一个原型属性:prototype
  • 对于普通函数没用,对构造函数来说才有用;
  • 构造函数是对象工厂,是用来创建对象的;
  • 对象也叫实例,实例:实际案例;
  • js中没有’类‘的概念,它是基于原型的语言,所以可简单的将构造函数当成类;
  • 构造函数必须使用new来调用,普通函数不用
  • new的过程就是类的实例化过程,就是创建一个对象的过程;
  • 创建对象的过程,就叫类的实例化;
  • 实例的原型永远指向它的构造函数的原型,实例的原型从构造函数的原型继承成员(属性/方法);
  • 需要被所有类实例共享的成员,应该写到构造函数的原型上;
  • 如果当前实例类中存在和原型链上同名的属性,当前类的属性优先级最大;
  • 属性通常不应该共享,它是却分不同对象的标志,方法更适合共享

    类与继承

    1. class User1 {
    2. //构造方法
    3. constructor(name, email) {
    4. this.name = name;
    5. this.email = email;
    6. }
    7. // 原型方法
    8. show() {
    9. return {
    10. name: this.name,
    11. email: this.email,
    12. }
    13. }
    14. //静态方法
    15. static fetch() {
    16. //静态方法中的this指向类
    17. return this.hello(this.userName)
    18. }
    19. static userName = 'usernames';
    20. static hello(name) {
    21. return 'Hello' + name;
    22. }
    23. }
    24. let user1 = new User1('admin', 'admin@qq.com');
    25. console.log(user1.show());
    26. console.log(User1.fetch());

    继承

    surper()调用父类构造方法,确定this指向

  • 子类扩展父类的功能,可以拥有自己的属性或方法;
  • 子类中可以访问父类的构造方法,原型方法,静态方法,不能访问私有成员
    1. class Child extends User1 {
    2. constructor(name, email, gender) {
    3. //surper()调用父类构造方法,确定this指向
    4. super(name, email)
    5. this.gender = gender;
    6. }
    7. show() {
    8. return {
    9. name: this.name,
    10. email: this.email,
    11. gender: this.gender,
    12. }
    13. }
    14. }
    15. const child = new Child('孩子', 'admin@haizi.com', '男')
    16. console.log(child.show());

    函数知识

    函数:函数是代码复用的手段
    变量:变量是数据复用的手段
    声明:
    1. function 函数名(){
    2. 函数体
    3. }
    调用:
    函数名();
    …rest剩余参数
    1. function fun(...arr){
    2. console.log(arr)
    3. //累加器
    4. let res=0;
    5. for(let i of arr){
    6. res +=i;
    7. }
    8. console.log(res);
    9. }
    10. fun(1,2,3,4,5);

    … 什么时候是归并,什么时候是展开?

  • 在函数的参数中是…rest归并;
  • 在函数的调用的参数中是 展开
    函数默认是单值返回
    如果要返回多个值,可以使用引用类型,对象,数组;
    1. function fun(){
    2. return {status:1,message:'成功'}
    3. }
    4. console.log(fun());

箭头函数

匿名函数:let fun=function(){};
箭头函数是用来简化匿名函数的语法糖;
()=>{};
1、如果只有一条语句 可以不写return和{};

  1. let fun = function() {
  2. return [1, 2, 3, 4];
  3. }
  4. console.log(fun());
  5. // 简写
  6. let fun1 = () => [1, 2, 3, 4];
  7. console.log(fun1());


2、只有一个参数时()可以省略,没有参数不能省略,多个参数时必须要写

  1. let fun=name=>[name];

3、当有多条语句时,函数体的{}不能省

DOM

用css选择器获取dom元素

html是一个树形结构化文档,
文档的结构叫dom;

  • document.querySelector(),返回匹配的元素集合中的第一个元素(返回一个);

  • document.querySelectorAll(),返回匹配的元素集合所有成员;

    1. const lis = document.querySelectorAll('.item');
    2. lis.forEach(item => {
    3. console.log(item);
    4. // style设置元素的内联样式
    5. item.style.color = 'red'
    6. })

    模拟jquery 选择器

    1. const $ = selector => document.querySelectorAll(selector);
    2. console.log($('.item'));


    支持伪类 例获取第一个元素

    1. let first = document.querySelectorAll('.item:first-of-type');
    2. console.log(first);

    常用快捷方式

  • html document.documentElement;
  • head document.head;
  • body document.body;
  • titlle document.title;

dom元素的增删改查

  • 1、创建元素 createElement();
    1. const ul = document.querySelector('ul');
    2. // 创建元素
    3. const li = document.createElement('li');
    4. li.innerText = 'item6';
    5. ul.appendChild(li);
    6. let html = "<li style='color:red'>item7</li>";
    7. //将html字符串之间解析为dom元素
    8. ul.insertAdjacentHTML('beforeEnd', html);
    9. // 如果大量添加元素应该使用文档片段完成
    10. //文档片段
    11. const frag = new DocumentFragment();
    12. for (let i = 0; i < 5; i++) {
    13. const li = document.createElement('li');
    14. li.textContent = 'hello' + (i + 1);
    15. //将生成的节点先临时挂载倒文档片段中;
    16. frag.appendChild(li);
    17. }
    18. ul.appendChild(frag);
    19. html = `<li style='color:blue'>demo1</li><li style='color:blue'>demo2</li><li style='color:blue'>demo3</li><li style='color:blue'>demo4</li>`;
    20. ul.insertAdjacentHTML('afterBegin', html);
  • 2、更新
    1. let h3 = document.createElement('h3');
    2. h3.innerText = '晚上好';
    3. document.querySelector('li:nth-of-type(3)').replaceWith(h3);
    4. let h2 = document.createElement('h3');
    5. h2.innerText = '晚上好';
    6. ul.replaceChild(h2, document.querySelector('li:last-of-type'));
  • 3、删除
    1. ul.removeChild(document.querySelector('li:first-of-type'));
  • 4、查询
    1. // 遍历查询
    2. console.log(ul.children);
    3. // 子元素的数量
    4. console.log(ul.children.length);
    5. console.log(ul.childElementCount);
    6. // 第一个子元素
    7. console.log(ul.firstElementChild);
    8. // 最后一个子元素
    9. console.log(ul.lastElementChild);
    10. // 父节点
    11. console.log(ul.lastElementChild.parentElement);
    12. //前一个兄弟
    13. const jiu = document.querySelector('li:nth-of-type(9)');
    14. jiu.style.background = 'yellow';
    15. console.log(jiu.previousElementSibling.innerHTML);
    16. // 后一个兄弟
    17. console.log(jiu.nextElementSibling.innerHTML);

    classList对象

    获取元素的类名
    1. let p = document.querySelector('p');
    2. console.log(p.className);
    3. console.log(p.id);

    classList就是专门来操作类的
  • 添加类 x.classList.add(xx);
    1. p.classList.add('green');
  • 移除类
    x.classList.remove(xx);
  • 替换类
    x.classList.replace(要替换的,替换成);
  • 动态切换类,如果有则删除,没有则添加 x.classList.toggle()

    dataset对象

    专用于访问自定义的标签属性
    1. <input type="text" class="username" value="个人简介" data-email="admin@admin.com">;
    2. let v = document.querySelector('.username');
    3. console.log(v.value);
    4. console.log(v.dataset.email);
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议