Blogger Information
Blog 40
fans 0
comment 1
visits 34285
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript初学习之构造函数、类与继承、获取dom元素、dom元素的增删改查的认识与简用
景云
Original
559 people have browsed it

1.构造函数

1.1 构造函数的原型
  1. 任何一个函数都有一个原型属性:prototype
  2. 原型属性对于普通函数没用,对构造函数来说才有用;
  3. 构造函数是“对象工厂”,用来创建对象的,对象也叫“实例”(实际案例);
  4. JS中没有类的概念,它是基于原型的语言,所以可简单的将构造函数当成类;
  5. 构造函数必须使用“new”来调用,普通函数不用;
  6. new的过程就是类的实例化过程,就是创建一个对象的过程;创建对象的过程,就叫“类的实例化”
  1. //声明一个构造函数
  2. function User(name,email){
  3. //1.创建一个新对象,用this来表示
  4. //const this=new User;伪代码,系统会自动创建并执行,写出来为了辅助理解
  5. // 2.初始化对象,给这个对象添加一个自定义属性,用来和其他实例进行区分
  6. this.name=name;
  7. this.email=email;
  8. // 3.返回这个对象
  9. // return this;伪代码,系统会自动创建并执行,写出来为了辅助理解
  10. }
  11. const user=new User("admin","ad@php.cn");
  12. console.log(user);
  13. // 实例的原型永远指向它的构造函数的原型,实例的原型从构造函数的原型继承成员(属性/方法)
  14. const user2=new User("tom","tom@php.cn");
  15. console.dir(User);
  16. console.log(user2.__proto__ === User.prototype);
  17. // 需要被所以类实例共享的成员,需要写到构造函数的原型上
  18. User.prototype.nation="CHINA";
  19. console.log(user.nation,user2.nation);
  20. // 属性通常通常不应该共享的,它是区分不同对象的标志,方法更适合共享
  21. User.prototype.show=function (){
  22. return {name:this.name,email:this.email}
  23. };
  24. console.log(user.show());
  25. console.log(user2.show());

2.类与继承

  1. const User=function(name,email){
  2. this.name=name;
  3. this.email=email;
  4. }
  5. User.prototype.show=function (){
  6. return {name:this.name,email:this.email};
  7. };
  8. const user=new User("景云","Jy@php.cn");
  9. console.log(user.show());
  10. // es6中的类来改写上面
  11. class User2{
  12. //构造方法
  13. constructor(name,email){
  14. this.name=name;
  15. this.email=email;
  16. };
  17. //原型方法,要是用实例/对象来调用
  18. show (){
  19. return {name:this.name,email:this.email,age:this.#age};
  20. };
  21. //私有成员,只能在本类中使用,类外和子类都不能使用
  22. #age=28;
  23. }
  24. const user2=new User2("景云","Jy@php.cn");
  25. console.log(user2.show());
  26. // 类的继承
  27. class Child extends User2{
  28. // 子类扩展父类的功能,可以拥有自己的属性或方法
  29. // 子类中可以访问父类的构造方法、原型方法、静态方法,不能访问私有成员
  30. constructor(name,email,gender){
  31. // super()调用父类构造方法,确定this指向
  32. super(name,email);
  33. this.gender=gender;
  34. }
  35. show (){
  36. return {name:this.name,email:this.email,gender:this.gender };
  37. };
  38. }
  39. const child=new Child("景云c","Jyc@php.cn","男");
  40. console.log(child.show());

3.获取dom元素

  1. <ul id="list">
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3</li>
  5. <li class="item">item4</li>
  6. <li class="item">item5</li>
  7. </ul>
  1. // 使用css选择器获取dom元素是最直观的
  2. // 3.1 获取所以满足条件的元素
  3. const list=document.querySelectorAll("#list li");
  4. console.log(list);
  5. // 返回结果中Nodelist是浏览器内置的集合类型,属于类数组;Array.form()、...rest都可以将其转为真正的数组。
  6. let list2=Array.from(list);
  7. console.log(list2);
  8. let list3=[...list];
  9. console.log(list3);
  10. // Nodelist可以用forEach()遍历
  11. list.forEach(function(item,len,arr){
  12. console.log(item);
  13. });
  14. console.log("=================");
  15. // 3.2 获取满足条件的第一个元素
  16. let first=document.querySelector("#list li");
  17. console.log(first);

4.dom元素的增删改查

  1. <ul id="list">
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. </ul>
4.1 创建元素
  1. const ul=document.querySelector("#list");
  2. const li=document.createElement("li");
  3. //parent.appendChild(newEl),添加到页面
  4. ul.appendChild(li);
  5. li.innerText="item3";
  6. // 也可以使用html字符串,将html字符串直接解析为dom元素
  7. let htmlStr="<li style='color:red'>item4</li>";
  8. ul.insertAdjacentHTML("beforeend",htmlStr);
  9. // 如果大量添加元素应该使用文档片段完成
  10. const frag=new DocumentFragment();
  11. for(let i=0;i<4;i++){
  12. const li=document.createElement("li");
  13. li.textContent="hello"+(i+1);
  14. // 将生成的节点先临时挂载到文档片段中
  15. frag.appendChild(li);
  16. }
  17. ul.appendChild(frag);
4.2 更新元素
  1. const ul=document.querySelector("#list");
  2. const li=document.createElement("li");
  3. let h6=document.createElement("h6");
  4. let h3=document.createElement("h3");
  5. h6.innerText="你好呀6";
  6. h3.innerText="你好呀3";
  7. document.querySelector("li:nth-of-type(3)").replaceWith(h3);
  8. // 也可以换一种写法 ul.replaceChild(h6,document.querySelector("li:last-of-type"));
4.3 移出元素
  1. const ul=document.querySelector("#list");
  2. const li=document.createElement("li");
  3. ul.removeChild(document.querySelector("li:nth-of-type(5)"));
4.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.firstElementChild.parentElement);
  12. // 前一个兄弟
  13. const qian=document.querySelector("li:nth-of-type(4)");
  14. qian.style.background="yellow"; console.log(qian.previousElementSibling.innerHTML);//打印前一个兄弟元素的html
  15. // 后一个兄弟
  16. console.log(qian.nextElementSibling.innerHTML);//打印后一个兄弟元素的html

附:图示

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!