批改状态:合格
老师批语:
fn=function(a,b){return function(c){return a+b+c;}}
fn = function (a) {return function (b) {return function (c) {return a + b + c;};};};
上述函数还可简化成 fn=a=>b=>c=>a+b+c
player={isnewplayer:1,data: {accont:"376220510",password:"6350187",level:100,job:"战士"},get job(){return this.data.job;},set job(newjob){this.data.job=newjob;},}console.log(player.job);player.job="法师";console.log(player.job);
上述代码中,使用player.job访问了player对象中的get job方法。这种将方法伪装成属性的方式称之为访问器属性
class player{account="admin";password="123456";sex="man";level="100";job="战士";hp="20000";mp="2000";constructor(account,password,sex,level,job){this.account=account;this.password=password;this.sex=sex;this.level=level;this.job=job;}setinfo(level){this.level=level;this.hp=level*200;this.mp=level*20;}}let player1=new player("376220510","123456","女",1,"战士");console.log(player1.hp);player1.setinfo(1);console.log(player1.hp);
上述代码,创建了一个player类,并且使用new创建了player1这个实例
let [name, email] = ["朱老师", "498668472@qq.com"];console.log(name, email);
let { id, lesson, score } = { id: 1, lesson: "js", score: 80 };console.log(id, lesson, score);let {...obj} ={ id: 1, lesson: "js", score: 80 };console.log(obj);
function getUser({id,name,email}) {console.log(id,name,email);}getUser({id:123, name:'张三',email:'zs@php.cn'})
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>浏览器中的js</title></head><body><button onclick="console.log('hello world');">按钮1</button><button onclick="setBg(this)">按钮2</button><script>function setBg(ele) {document.body.style.backgroundColor = "wheat";ele.style.backgroundColor = "yellow";ele.textContent = "保存成功";}</script></body></html>
<script src="outer.js"></script>这样即可
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>获取dom元素</title></head><body><ul class="list"><li class="item">item1</li><li class="item">item2</li><li class="item">item3</li><li class="item">item4</li><li class="item">item5</li></ul><script>console.log(document);const items = document.querySelectorAll(".list > .item");console.log(items);for (let i = 0, length = items.length; i < length; i++) {items[i].style.color = "red";}</script></body></html>
上述代码中使用document.querySelectorAll(".list > .item");获得了所有li元素
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>获取dom元素</title></head><body><ul class="list"><li class="item">item1</li><li class="item">item2</li><li class="item">item3</li><li class="item">item4</li><li class="item">item5</li></ul><script>const first = document.querySelector(".list > .item");console.log(first === items[0]);first.style.backgroundColor = "yellow";const three = document.querySelector(".list > .item:nth-of-type(3)");three.style.backgroundColor = "wheat";</script></body></html>
上述代码中使用document.querySelector(".list > .item");获得了第一个li元素
使用document.querySelector(".list > .item:nth-of-type(3)");获得了第三个li元素
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号