JavaScript变量和作用域 -- 笔记 - 图2review0601

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>查漏补缺</title>
  6. </head>
  7. <body>
  8. </body>
  9. <script type="text/javascript">
  10. // var a;
  11. // 什么是变量
  12. // 数据:4 'str' true {} []
  13. // 一次:哪里需要写哪里
  14. // 多次:保存数据的容器
  15. // 1、什么是标识符
  16. // 变量、函数、属性的名字,或者函数的参数
  17. // 2、标识符的命名规则
  18. // -由字母,数字,下划线(_)或美元$组成
  19. // -不能以数字开头
  20. // -不能使用关键字,保留字作为标识符
  21. // var nun = 4;num NUM 严格区分大小写
  22. // 关键字:if for
  23. // 保留字:class
  24. // 基本数据:4 'str' true undefined null
  25. // 引用数据:[]数组 {}对象
  26. // 区别:
  27. // 1、基本数据类型值不可以修改
  28. // 2、引用数据类型值可以修改
  29. // var num = 4;
  30. // num = 3;//覆盖
  31. // var str = 'string';
  32. // var anotherStr = str.replace('s','');
  33. // console.log(str+"|"+anotherStr);
  34. // var person = {};
  35. // console.log(person);
  36. // //var str = 'xiao ming';
  37. // person.name='xiao ming';
  38. // person.sex='male';
  39. // person.sex='female';
  40. // person.family=['ming ba','ming ma'];
  41. // delete person.sex;
  42. // console.log(person);
  43. // var person = 'person';
  44. // person.name='xiao ming';
  45. // console.log(person.name);//
  46. // var str = 'string';//包装类 -- 才可以调用replace方法
  47. // var anotherStr = str.replace('s','');
  48. // console.log(str+"|"+anotherStr);
  49. // 1 -- Number
  50. // 'str' -- String
  51. // 数据到底保存在哪里? -- 堆栈
  52. // 1、堆内存
  53. // 空间大小不固定 -- 别墅
  54. // 2、栈内存
  55. // 有序排列的,会分成一个一个小房间,每一个小房间是存储变量的,
  56. // 但是栈内存每一个大小都是固定的,不能扩建 -- 公寓式
  57. // var xmScore = 4;
  58. // var xhScore = 4;
  59. // console.log(xmScore === xhScore);
  60. // var xm={
  61. // age:18,
  62. // score:4
  63. // };
  64. // var xh={
  65. // age:18,
  66. // score:4
  67. // };
  68. // console.log(xm === xh);
  69. // var xm={
  70. // age:18,
  71. // score:4
  72. // };
  73. // var xh = xm;
  74. // console.log(xm === xh);
  75. //需求:
  76. // var xm={
  77. // age:18,
  78. // score:4,
  79. // str:'ss'
  80. // };
  81. // var xh={
  82. // score:4,
  83. // age:18
  84. // };
  85. // // console.log(xm === xh);
  86. // function equalObjs(a,b){//a和b 就是对象
  87. // console.log(a);
  88. // console.log(b);
  89. // for(var p in a){
  90. // console.log(a[p]+"-"+p);
  91. // console.log(b[p]+"-"+p);
  92. // if (a[p]!==b[p]) {return false;}
  93. // }
  94. // return true;
  95. // }
  96. // console.log(equalObjs(xm,xh));
  97. // function equalsArrays(a,b){
  98. // if (a.length!=b.length) {return false;}
  99. // for(var i=0;i<a.length;i++){
  100. // if (a[i]!==b[i]) {return false;}
  101. // }
  102. // return true;
  103. // }
  104. // //复制
  105. // var xmscore = 4;
  106. // var xhscore = xmscore;
  107. // xhscore++;
  108. // console.log(xhscore);//5
  109. // // console.log(xmscore);//4 基本数据类型的值互不干涉
  110. // var xm={
  111. // age:18,
  112. // score:4
  113. // };
  114. // var xh = xm;
  115. // console.log(xh.score);//4
  116. // xh.score++;
  117. // console.log(xh.score);//5
  118. // console.log(xm.score);//5 因为他们同时指向一个引用
  119. // var xm={
  120. // age:18,
  121. // score:4
  122. // };
  123. // function copyObj(obj){
  124. // var newObj={};
  125. // for(var p in obj){
  126. // newObj[p]=obj[p];
  127. // }
  128. // return newObj;
  129. // }
  130. // xh=copyObj(xm);
  131. // console.log(xh);
  132. // console.log(xh === xm);
  133. //参数传递
  134. // function fn(a,b){//形参
  135. // return a+b;
  136. // }
  137. // fn(1,2);//实参
  138. // function addTen(num){
  139. // return num+10;
  140. // }
  141. // var score = 10;
  142. // console.log(addTen(score));
  143. // function setName(obj){
  144. // return obj.name="xm";
  145. // }
  146. // var person={};
  147. // setName(person);
  148. // console.log(person.name);//xm
  149. // obj=person//他们指向同一个地址
  150. //检测
  151. // 4
  152. // 'string'
  153. // true
  154. // undefined
  155. // null
  156. // []
  157. // {}
  158. // function
  159. // RegExp
  160. //typeof m typeof(m)
  161. // console.log(typeof 4);
  162. // console.log(typeof(4));
  163. // console.log(typeof 'string');
  164. // console.log(typeof true);
  165. // console.log(typeof undefined);
  166. // console.log(typeof null);
  167. // console.log(typeof []);
  168. // console.log(typeof {});
  169. // console.log(typeof function(){});
  170. // console.log(typeof /a/);
  171. // console.log(typeof Array);
  172. // //instanceof 表示前面是否是后面的实例
  173. // console.log([] instanceof Array);
  174. // console.log([] instanceof Object);
  175. // console.log({} instanceof Object);
  176. // console.log({} instanceof Array);
  177. // //instanceof不能和基本数据类型一起使用
  178. // console.log(1 instanceof Number);
  179. // 变量的
  180. // 作用:起作用
  181. // 域:区域和范围
  182. // 1、变量的生命周期
  183. // 2、访问到变量
  184. // 作用域
  185. // 全局作用域
  186. // 局部作用域:函数作用域
  187. // var name = 'xm';
  188. // function fn(argument){
  189. // var sex = 'male';
  190. // }
  191. // console.log(sex);
  192. // fn();//xm
  193. // var name;
  194. // console.log(name);
  195. // var name = 'm';
  196. // var age = 18;x
  197. // function fn(){
  198. // // var name;
  199. // console.log(name);//undefined js的预解析机制
  200. // var name = 'xh';
  201. // var age = 10;
  202. // }
  203. // fn();//xh
  204. // console.log(a);
  205. // var a = 1; 变量提升
  206. // console.log(a);
  207. // console.log(a);//undefined
  208. // a = 1;
  209. // console.log(a);//1
  210. // console.log(a);
  211. // a = 1;//报错
  212. // var a = 1;//全局变量
  213. // var a = 1;
  214. // function fn(){
  215. // console.log(a);//undefined
  216. // var a = 2;
  217. // }
  218. // fn();
  219. // console.log(a);//1
  220. // var a = 1;
  221. // function fn(){
  222. // console.log(a);//1 这里的fn作用域中没有找到var定义 所以只能往外找,所以输出第一个a = 1
  223. // a = 2;//由于他没有var定义,所以这里的a是全局变量 覆盖
  224. // }
  225. // fn();
  226. // console.log(a);//2
  227. // var a = 1;
  228. // function fn(a){//fn作用域中的局部变量 var a;
  229. // console.log(a);//undefined 预解析
  230. // a = 2;//这里的a修改局部变量
  231. // console.log(a);
  232. // }
  233. // fn();
  234. // console.log(a);//1
  235. // var a = 1;
  236. // function fn(a){
  237. // console.log(a);//1
  238. // var a = 2;//这里的a修改局部变量
  239. // }
  240. // fn(a);//读取的是全局a所以第一个a输出1
  241. // console.log(a);//1
  242. </script>
  243. <!-- <script type="text/javascript">
  244. console.log(a);//1
  245. </script> -->
  246. </html>