批改状态:合格
老师批语:
实现一个迭代器
// 手写一个迭代器function myIterator(data) {// 迭代器中必须要有一个next()let i = 0;return {next() {// done表示遍历是否完成// value表示当前遍历的数据// let done=i>=data.length?false:true;let done = i >= data.length;let value = !done ? data[i++] : undefined;return {done,value};}}}let interator = myIterator(['a', 'b', 'c']);console.log(interator.next());console.log(interator.next());console.log(interator.next());console.log(interator.next());

属性通常不应该共享,它是却分不同对象的标志,方法更适合共享
class User1 {//构造方法constructor(name, email) {this.name = name;this.email = email;}// 原型方法show() {return {name: this.name,email: this.email,}}//静态方法static fetch() {//静态方法中的this指向类return this.hello(this.userName)}static userName = 'usernames';static hello(name) {return 'Hello' + name;}}let user1 = new User1('admin', 'admin@qq.com');console.log(user1.show());console.log(User1.fetch());

surper()调用父类构造方法,确定this指向
class Child extends User1 {constructor(name, email, gender) {//surper()调用父类构造方法,确定this指向super(name, email)this.gender = gender;}show() {return {name: this.name,email: this.email,gender: this.gender,}}}const child = new Child('孩子', 'admin@haizi.com', '男')console.log(child.show());

调用:
function 函数名(){函数体}
function fun(...arr){console.log(arr)//累加器let res=0;for(let i of arr){res +=i;}console.log(res);}fun(1,2,3,4,5);

… 什么时候是归并,什么时候是展开?
function fun(){return {status:1,message:'成功'}}console.log(fun());
匿名函数:let fun=function(){};
箭头函数是用来简化匿名函数的语法糖;
()=>{};
1、如果只有一条语句 可以不写return和{};
let fun = function() {return [1, 2, 3, 4];}console.log(fun());// 简写let fun1 = () => [1, 2, 3, 4];console.log(fun1());

2、只有一个参数时()可以省略,没有参数不能省略,多个参数时必须要写
let fun=name=>[name];
3、当有多条语句时,函数体的{}不能省
html是一个树形结构化文档,
文档的结构叫dom;
document.querySelector(),返回匹配的元素集合中的第一个元素(返回一个);
document.querySelectorAll(),返回匹配的元素集合所有成员;
const lis = document.querySelectorAll('.item');lis.forEach(item => {console.log(item);// style设置元素的内联样式item.style.color = 'red'})

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

支持伪类 例获取第一个元素
let first = document.querySelectorAll('.item:first-of-type');console.log(first);

const ul = document.querySelector('ul');// 创建元素const li = document.createElement('li');li.innerText = 'item6';ul.appendChild(li);let html = "<li style='color:red'>item7</li>";//将html字符串之间解析为dom元素ul.insertAdjacentHTML('beforeEnd', html);// 如果大量添加元素应该使用文档片段完成//文档片段const frag = new DocumentFragment();for (let i = 0; i < 5; i++) {const li = document.createElement('li');li.textContent = 'hello' + (i + 1);//将生成的节点先临时挂载倒文档片段中;frag.appendChild(li);}ul.appendChild(frag);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>`;ul.insertAdjacentHTML('afterBegin', html);

let h3 = document.createElement('h3');h3.innerText = '晚上好';document.querySelector('li:nth-of-type(3)').replaceWith(h3);let h2 = document.createElement('h3');h2.innerText = '晚上好';ul.replaceChild(h2, document.querySelector('li:last-of-type'));

ul.removeChild(document.querySelector('li:first-of-type'));

// 遍历查询console.log(ul.children);// 子元素的数量console.log(ul.children.length);console.log(ul.childElementCount);// 第一个子元素console.log(ul.firstElementChild);// 最后一个子元素console.log(ul.lastElementChild);// 父节点console.log(ul.lastElementChild.parentElement);//前一个兄弟const jiu = document.querySelector('li:nth-of-type(9)');jiu.style.background = 'yellow';console.log(jiu.previousElementSibling.innerHTML);// 后一个兄弟console.log(jiu.nextElementSibling.innerHTML);

let p = document.querySelector('p');console.log(p.className);console.log(p.id);

p.classList.add('green');
<input type="text" class="username" value="个人简介" data-email="admin@admin.com">;let v = document.querySelector('.username');console.log(v.value);console.log(v.dataset.email);

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号