博客列表 >07-12 作业

07-12 作业

︷肉園耔︷
︷肉園耔︷原创
2021年08月02日 23:09:15441浏览

代码区
— 作用域和闭包

  1. <script>
  2. let staff={
  3. id:1,
  4. name:"朱老师",
  5. age:20,
  6. setAge(age){
  7. if(age >=18 && age <60 ){
  8. return (this.age=age);
  9. }
  10. console.log("年龄:(18-60)之间");
  11. }, };
  12. console.log(‘age=’,staff.age);
  13. console.log(‘name’,staff.name);
  14. staff.setAge(65);
  15. let staff={
  16. data:{id:1,name:”朱老师”,age:20,},
  17. setAge(age){if(age>=18 && age <60){
  18. return (this.data.age=age);
  19. }
  20. console.log(“年龄:(18-60)之间”);
  21. },
  22. };
  23. console.log(staff.data.age);
  24. staff.setAge(40);
  25. console.log(staff.data.age);
  26. 访问属性
  27. 1.读属性
  28. 2.写属性
  29. let staff={
  30. data:{id:1,name:”朱老师”,age:20},
  31. //读取的”访问起属性”
  32. get age(){
  33. console.log(“aaa”);
  34. return this.data.age;
  35. },
  36. set age(age){if(age >=18 && age<60){
  37. return(this.data.age=age);
  38. }
  39. console.log(“年龄:(19-60)之间”);
  40. }
  41. };
  42. console.log(staff.age);
  43. staff.age=81;
  44. console.log(“age=”,staff.age);
  45. 块作用域
  46. if(true){
  47. var a =10;
  48. let a=15;
  49. console.log(“a”,a);
  50. }
  51. console.log(“a=”,a);
  52. function demo1(a){
  53. let b=20;
  54. console.log(“a=%d,b=%d”, a , b);
  55. }
  56. demo1(10);
  57. console.log(15,31);
  58. function parent(){
  59. let m =500;
  60. return function (a){
  61. let b=2;
  62. console.log(a,b,m);
  63. };
  64. }
  65. let f =parent();
  66. console.log(f);
  67. f(4);
  68. console.log(parent());
  69. parent(4);
  70. //push推动 从尾部添加字母
  71. let arr=[];
  72. console.log(arr.push(“a”));
  73. console.log(arr);
  74. console.log(arr.push(“b”,”c”,”d”));
  75. console.log(arr);
  76. //pop(无参数) 从尾部删除
  77. console.log(arr.pop());
  78. console.log (arr);
  79. unshift从头部添加
  80. console.log(arr.unshift(1,2,3));
  81. console.log(arr);
  82. console.log (arr.shift());
  83. console.log(arr);
  84. arr=["html" ,"css","js"];
  85. let res=arr.join();
  86. console.log(arr);
  87. res=arr.join("---");
  88. console.log(res);
  89. les= arr.join("</li><li>");
  90. console.log(les);
  91. console.log('<ul><li>$<res></ul></li>');
  92. //document.body.insertAdiacentHTML("beforeend",html);
  93. //cancat():
  94. console.log("hello".concat("php.cn"));
  95. console.log(["hello"].concat(["php.cn"]));
  96. //splice()实现数组的增删改查的操作
  97. arr=[1,2,3,4,5];
  98. res =arr.splice(3,1);
  99. console.log(res);
  100. //res=arr.splice(2,0,"html","css");
  101. res=arr.splice(3,0, ...["php","js"]);
  102. console.log(res);
  103. console.log(arr);
  104. //更新操作,即删除又新增
  105. res=arr.splice(2,1,"javascript");
  106. console.log(res);
  107. console.log(arr);
  108. //forEach() 对数组中的每一个成员使用一个回调方法进行处理
  109. const items = document.querySelectorAll("li");
  110. items.forEach(function(item){
  111. item.style.color="red";
  112. })
  113. document.querySelectorAll("li").forEach(xxx=>(xxx.style.color="red"));
  114. //filter()对数组中的每个使用回调方法进行处理,只返回数据为true的元素
  115. arr=[1,2,3,4,5,6,7,8];
  116. res=arr.filter(a=>!(a%2));
  117. console.log(res);
  118. reduce();归并操作,将数组中的所有元素指定的回调处理一个单一结果返回
  119. arr=[1,2,3,4,5];
  120. res=arr.reduce(function(prev,curr){
  121. console.log(prev,curr);
  122. return prev+curr;
  123. });
  124. res=arr.reduce((prve,curr)=>prve+curr);
  125. console.log(res);
  126. arr=[5,6,7,8,9,12,13,14,15];
  127. res=arr.reduce(function(prev,curr){
  128. console.log(prev,curr);
  129. return prev+curr;
  130. });
  131. res=arr.reduce((prve,curr)=>prve+curr);
  132. console.log(res);
  133. user={
  134. 1:1,
  135. 2:"name",
  136. 3:"admin@php.cn",
  137. length:3,
  138. }
  139. for(let item of Array.from(user)){
  140. console.log(item);
  141. }
  142. /* let staff={
  143. id:1,
  144. name:”朱老师”,
  145. age:20,
  146. setAge(age){
  147. if(age >=18 && age <60 ){
  148. return (this.age=age);
  149. }
  150. console.log(“年龄:(18-60)之间”);
  151. },
  152. };
  153. console.log(‘age=’,staff.age);
  154. console.log(‘name’,staff.name);
  155. staff.setAge(65);
  156. let staff={
  157. data:{id:1,name:”朱老师”,age:20,},
  158. setAge(age){if(age>=18 && age <60){
  159. return (this.data.age=age);
  160. }
  161. console.log(“年龄:(18-60)之间”);
  162. },
  163. };
  164. console.log(staff.data.age);
  165. staff.setAge(40);
  166. console.log(staff.data.age);
  167. /*
  168. 访问属性
  169. 1.读属性
  170. 2.写属性
  171. */
  172. let staff={
  173. data:{id:1,name:”朱老师”,age:20},
  174. //读取的”访问起属性”
  175. get age(){
  176. //console.log(“aaa”);
  177. return this.data.age;
  178. },
  179. set age(age){if(age >=18 && age<60){
  180. return(this.data.age=age);
  181. }
  182. console.log(“年龄:(19-60)之间”);
  183. }
  184. };
  185. console.log(staff.age);
  186. staff.age=81;
  187. console.log(“age=”,staff.age);
  188. //块作用域
  189. if(true){
  190. var a =10;
  191. let a=15;
  192. console.log(“a”,a);
  193. }
  194. console.log(“a=”,a);
  195. function demo1(a){
  196. let b=20;
  197. console.log(“a=%d,b=%d”, a , b);
  198. }
  199. demo1(10);
  200. console.log(15,31);
  201. function parent(){
  202. let m =500;
  203. return function (a){
  204. let b=2;
  205. console.log(a,b,m);
  206. };
  207. }/
  208. /let f =parent();
  209. console.log(f);
  210. f(4);
  211. const items= document.querySelectorAll(‘.list .item’);
  212. console.log(items);
  213. Array.from(items).forEach(item => (item.style.color=”red”));
  214. console.log(items[0]);
  215. items[0].style.background=”yellow”;
  216. //返回满足条件的元素集合第一个,常用来返回唯一元素
  217. const first = document.querySelector(“.list .item “);
  218. console.log(first);
  219. console.log(first===items[0]);
  220. const ul = document.querySelectorAll(“.list”);
  221. console.log(ul);
  222. const ul=document.querySelector(“.list”);
  223. console.log(ul);
  224. body
  225. const body = document.querySelector(“body”);
  226. body.style.backgroundColor=”lightcyan”;
  227. console.log(document.head);
  228. let $ =function(selector){
  229. return document.querySelector(selector);
  230. }
  231. console.log($(“ul”));
  232. let $ =selector=>document.querySelector(selector);
  233. let $ =selector=>document.querySelector(selector);
  234. console.log($(“body”).style.backgroundColor=”lightcyan”);
  235. //表单获取的特殊性
  236. const form= document.querySelector(“#IN”);
  237. console.log(form);
  238. document.forms获取表单ID选择器和nama
  239. console.log(document.forms.IN);
  240. console.log(document.forms.FM);
  241. console.log(document.forms.IN === document.forms.FM);
  242. console.log(document.forms.IN.login.value);
  243. //DOM树中的所有内容成为节点
  244. //节点类型:元素,文本,文档
  245. let ul=document.querySelector(‘.list’);
  246. console.log(ul.childNodes);
  247. //children
  248. //Array.from(ul.children).forEach(item=>(item.style.color="red"));
  249. //[...ul.children].forEach(item=>(item.style.color="red"));
  250. //console.log(([...ul.children].shift().style.color="blue"));
  251. console.log((ul.firstElementChild.style.color="blue"));
  252. console.log(ul.lastElementChild.style.color="blue");
  253. //第二个元素
  254. console.log(ul.firstElementChild.nextElementSibling.style.color="red");
  255. //第四个元素
  256. console.log(ul.lastElementChild.previousElementSibling.style.color="blue");
  257. //第三个元素
  258. const li =document.querySelector(".item:nth-of-type(3)");
  259. console.log(li);
  260. //创建只在内存中,不在页面中 creeateElement
  261. let div=document.createElement(“div”);
  262. let p =document.createElement(‘p’);
  263. p.textContent=”Hello World”;
  264. //将元素渲染到页面中
  265. document.body.append(p);
  266. console.log(document.querySelector(‘div’).id);
  267. console.log(document.querySelector(‘div’).data-email);
  268. 对于这样的自定义属性,成为数据属性
  269. console.log(document.querySelector(“div”)[“data-email”]);
  270. //dataset专用获取’data-前缀的自定义’
  271. let email2=document.querySelector(“div”).dataset.email;
  272. let email1=document.querySelector(“div”).dataset.email;
  273. console.log(email2);
  274. console.log(email1);
  275. </script>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议