核心(Core) Javascript 学习手记 By shawl.qiu

78 篇文章 0 订阅
31 篇文章 0 订阅

核心(Core) Javascript 学习手记 By shawl.qiu


说明:
一字一句都是俺敲出来的, ^^. 转载请注明出处哦.

前些日子, 发现好些地方需要用到 Js 代码, 以前没怎么系统的学习过, 要用到的时候查一堆一堆的手册, 写得不好不说, 还很浪费时间, 真是不划算.
 
这阵子下决心, 一定要把 Js 这家伙搞定, 到目前为止, 基本上对 Js 的核心部分有了个小心得. 

对于 Js 的客户端功能还是停留于N久前的水平, 现在把 Core 部分搞定了, 接下来客户端想来应该不是问题...

强烈推荐以下相关资料: 
1. JavaScript: The Definitive Guide, 4th Edition
这本书咱啃的是 E 文版, 真不错, 不错的地方在于作者在书中讲了许多原理, 这是真功夫呀. 
2. Microsoft Windows 脚本技术
微软自家的 JScript && VBScript 手册. 

目录:
1. Javascript Notes By shawl.qiu
2. Javascript 正则表达式摘要
注: Javascript 的正则表达式我另外做了一个笔记, 过段时间再与大家分享其全部内容. 

shawl.qiu 
2006-10-20
  http://blog.csdn.net/btbtd

1. Javascript Notes By shawl.qiu
  1. linenum
  2.  
  3. CREATED BY STABX, AT 2006-9-5.
  4.  
  5. Javascript Notes By shawl.qiu
  6.  
  7. ---/-------------------------------------------------------------
  8.  
  9. -3.1 函数比其他代码优先执行
  10. -3. 优先级
  11. -2.4 同时初始化多个变量并赋相同值
  12. -2.3 在函数中使用 var 定义一个变量, 该变量将影响整个函数, 不分先后. 
  13. -2.2 全局变量与局部变量
  14. -2.1.2 普通定义变量 2
  15. -2.1.1 普通定义变量 1
  16. -2.1 普通定义变量
  17. -2. var 定义变量
  18. -1.5 in 操作符
  19. -1.4.2 true 等价 1, false 等价 0.
  20. -1.4 == 与 ===
  21. -1.4.1 NaN
  22. -1.3 0/0=?
  23. -1.2 类型转换
  24. -1.1 -- 与 ++
  25. -1.1.1
  26. -1. 运算符
  27.  
  28. 0. 语法
  29. 0.1 Switch
  30.  
  31. 1. 按钮
  32. 1.1 前进后退按钮
  33. 1.2 确认按钮
  34. 1.3 最原始也最实用的  UBB 代码输入方法——SCRIPT & FORM
  35. 1.4 Prompt
  36. 1.5.1 鼠标经过时选中,并隐藏原框的文字, 移开再显示文字 ——文本框
  37. 1.5 鼠标经过时选中 ——复选框 Focus()
  38. 1.6 鼠标经过自动提交表单
  39.  
  40. 2. 接收键盘事件
  41. 2.1 按任何键关闭窗口 
  42. 2.2 实时显示更改的图片链接
  43.  
  44. 3. 数组/Array
  45. 3.1 Javascript 结合 Asp 使用数组
  46.  
  47. 4. 控制图片大小
  48.  
  49. 5. 双击拷贝文本
  50.  
  51. 6. IE 全屏显示
  52.  
  53. 7. 文本框回车链接
  54.  
  55. 8. 顯示頁面所有元素
  56.  
  57. 9. 正则表达式
  58.  
  59. 10. 接收鼠标事件
  60. 10.1 单击鼠标右击关闭窗口 (我用左手)
  61. 10.2 鼠标移过选择表单项
  62.  
  63. 11. 字符串
  64. 11.1 检测字符串中是否出现某字符
  65.  
  66. 12. 随机数
  67. 12.1.1 生成 GUID 2
  68. 12.1 生成 GUID
  69.  
  70. 13. 时间操作
  71. 13.1 以毫秒为单位的倒计时跳转, 来源 CSDN
  72.  
  73. 14. switch
  74. 14.1 简单 switch
  75. 15 if 判断
  76. 15.1.2 简单 if 判断 3
  77. 15.1.1 简单 if 判断 2
  78. 15.1 简单 if 判断
  79. 15.2 if 判断的 三个 写法
  80. 15.3 判断执行文件使用网络还是使用本地协议
  81. 15.4 if...else 写法
  82.  
  83. 16. 简单 while
  84.  
  85. 16. while
  86.  
  87. 17. do while
  88. 17.1.2 简单 do while
  89. 17.1 简单 do while
  90.  
  91. 18. for
  92. 18.1 简单 for
  93. 18.2 简单 for 1
  94. 18.3 循环列出从 a - z 的字母
  95. 18.3 n阶乘
  96. 18.4 Fibonacci Numbers
  97.  
  98. 19. for in
  99. 19.1.1 简单 for in 1
  100. 19.1 简单 for in
  101. 19.2.1 for in 赋值 [数组] 1
  102. 19.2 for in 赋值 [数组]
  103.  
  104. 20. try, catch, finally
  105. 20.1.1 简单 try, catch 1
  106. 20.1 简单 try, catch
  107.  
  108. 21. with
  109. 21.1 简单with (另一写法)
  110. 21.1 简单 with
  111.  
  112. 22. break, continue
  113. 22.1 continue
  114. 22.2.1 break 标签
  115. 22.2 break 
  116.  
  117. 23. call, apply
  118. 23.1.1 找出数组中最大的数
  119. 23.1 apply
  120.  
  121. 24. object
  122. 24.1.1 创建简单对象 一
  123. 24.1.2 创建简单对象 二
  124. 24.1 创建简单对象
  125. 24.2.1 创建嵌套对象 1
  126. 24.2 创建嵌套对象
  127. 24.3 删除对象中的属性
  128. 24.4.1 遍历对象中的属性名 1
  129. 24.4 遍历对象中的属性名
  130. 24.5 obj.propertyIsEnumerable('ele')
  131. 24.6 obj.hasOwnProperty('ele')
  132.  
  133. 25. toString
  134. 25.1 使用 toString 列出数组中的元素
  135.  
  136. 26. Array()
  137. 26.0.1 简单创建数组, 并赋值 2
  138. 26.0 简单创建数组, 并赋值
  139. 26.0.3 创建数组中的数组
  140. 26.0.4 创建数组中的对象
  141. 26.0.2 简单创建数组
  142. 26.1 [array].push(), [array].pop()
  143. 26.2 数组排序
  144. 26.3 创建数组指定维数, 从 1 开始.
  145.  
  146. 27. RegExp
  147. 27.1 普通操作 1
  148. 27.2 test 语句
  149. 27.3 exec 语句
  150.  
  151. 28. new 语法
  152.  
  153. 29. isNaN
  154.  
  155. 30. Date()
  156. 30.1 输出年月日, 时分秒 的日期格式
  157.  
  158. 31. function 函数
  159. 31.1.1.1 三种定义函数的方法 1.1
  160. 31.10 在数组中存储函数, 并引用
  161. 31.1.1 三种定义函数的方法 1
  162. 31.1.2 三种定义函数的方法 2
  163. 31.1 三种定义函数的方法
  164. 31.2 在标签中写函数
  165. 31.3 创建嵌套函数
  166. 31.4.1 创建递归函数 1 
  167. 31.4 创建递归函数
  168. 31.5 创建 函数对象
  169. 31.6 在函数内部定义的函数只能在该函数内部调用
  170. 31.7 定义函数变量, 并赋值
  171. 31.8 变量引用函数
  172. 31.9 在对象中存储函数, 并引用
  173. 31.11 function.call()
  174. 31.12 在函数内定义的变量可以被下级内嵌函数调用 
  175.  
  176. 32. typeof 查看数据类型
  177. 32.1 
  178.  
  179. 33. obj.valueOf() 返回原始值
  180.  
  181. 34. Math 对象
  182. 34.1.1 生成 1 至 4 的随机数
  183. 34.1 Math.random();
  184. 34.2 求 x 的 n 次方
  185.  
  186. 35. delete 
  187.  
  188. 36. confirm() 确认框
  189. 36.1 带条件确认框
  190.  
  191. 37. window.open
  192.  
  193. 37. window
  194. 37.1 打开没有焦点的窗口
  195.  
  196. 38. return 返回值
  197. 38.1 return true
  198. 38.2 return undefined
  199.  
  200. 39. throw
  201. 39.1 简单产生错误
  202.  
  203. 40. arguments
  204. 40.1 判断 arguments.length
  205. 40.2 列出所有 argument 的值
  206. 40.3 arguments.callee / 调用函数本身
  207.  
  208. 41. .to******
  209. 41.1 toString
  210.  
  211. 42. parseInt() / parseFolat()
  212.  
  213. --------------------------------
  214.  
  215. 42. parseInt() / parseFolat()
  216. <script type="text/javascript">
  217. //<![CDATA[
  218.     var str='1.1322 this test ';
  219.     document.write(('str like this: ').bold()+str.fontcolor('red')+'<br/>');
  220.     document.write(('parseInt(str): ').bold()+parseInt(str)+'<br/>');
  221.     document.write(('parseFloat(str): ').bold()+parseFloat(str)+'<br/>');
  222.     
  223.     document.write(('parseInt(1111,2): ').bold()+parseInt(1111,2)+'<br/>');
  224.     document.write(('parseInt(1111,8): ').bold()+parseInt(1111,8)+'<br/>');
  225.     document.write(('parseInt(1111): ').bold()+parseInt(1111)+'<br/>');
  226.     document.write(('parseInt(1111,16): ').bold()+parseInt(1111,16)+'<br/>');
  227. //]]>
  228. </script>
  229.  
  230. 41. .to******
  231. <script type="text/javascript">
  232. //<![CDATA[
  233.     var num=12345.6789
  234.     
  235.     document.write(('parseInt(num): ').bold()+parseInt(num)+'<br/>');
  236.     
  237.     document.write(('num.toFixed(0): ').bold()+num.toFixed(0)+'<br/>');
  238.     document.write(('num.toFixed(2): ').bold()+num.toFixed(2)+'<br/>');
  239.     
  240.     document.write(('num.toExponential(2): ').bold()+num.toExponential(2)+'<br/>');
  241.     document.write(('num.toExponential(1): ').bold()+num.toExponential(1)+'<br/>');
  242.     
  243.     document.write(('num.toPrecision(1): ').bold()+num.toPrecision(1)+'<br/>');
  244.     document.write(('num.toPrecision(2): ').bold()+num.toPrecision(2)+'<br/>');
  245.     
  246. //]]>
  247. </script>
  248.  
  249. 41.1 toString
  250. <script type="text/javascript">
  251. //<![CDATA[
  252.     var num=123456789
  253.     
  254.     document.write(('num.toString(): ').bold()+num.toString()+'<br/>');
  255.     document.write(('num.toString(2): ').bold()+num.toString(2)+'<br/>');
  256.     document.write(("'0'+num.toString(8): ").bold()+'0'+num.toString(8)+'<br/>');
  257.     document.write(("'0x'+num.toString(16): ").bold()+'0x'+num.toString(16)+'<br/>');
  258.  
  259. //]]>
  260. </script>
  261.  
  262.  
  263.  
  264. 40. arguments
  265.  
  266. 40.1 判断 arguments.length
  267. <script type="text/javascript">
  268. //<![CDATA[
  269.     function t(a, b, c, d){
  270.         if(arguments.length!=4)document.write(('error! argument must be 4 element.').fontcolor('red')+' current arguments length: '+arguments.length+'<br/>');
  271.         else document.write(('this right: '+arguments.length).bold()+' arguments<br/>');
  272.     }
  273.     t();
  274.     t('a', 'b', 'c', 'd');
  275.     t('a', 'b', 'c', 'd', 'e');
  276. //]]>
  277. </script>
  278.  
  279. 40.2 列出所有 argument 的值
  280. <script type="text/javascript">
  281. //<![CDATA[
  282.     function t(){
  283.         for(var i=0; i<arguments.length; i++){
  284.             document.write(('arguments['+i+']').bold()+' = '+arguments[i].fontcolor('red')+'<br/>');
  285.         }
  286.     }
  287.     var temp='test';
  288.     t('aaa', 'bbb', 'ccc', 'ddd', 'eee', temp);
  289. //]]>
  290. </script>
  291.  
  292. 40.3 arguments.callee / 调用函数本身
  293. <script type="text/javascript">
  294. //<![CDATA[
  295.     var t=function(x){
  296.             document.write(x+'<br/>');
  297.             if(x>1){
  298.                 arguments.callee(x-1);
  299.             }
  300.         }
  301.         t(10);
  302. //]]>
  303. </script>
  304.  
  305. 39. throw
  306.  
  307. 39.1 简单产生错误
  308. <script type="text/javascript">
  309. //<![CDATA[
  310.     t(10);
  311.     t();
  312.     
  313.     function t(x){
  314.         if(typeof x==='undefined')throw new Error('x must be own a value');
  315.         document.write(x);
  316.     }
  317. //]]>
  318. </script>
  319.  
  320. 38. return 返回值
  321.  
  322. 38.1 return true
  323. <script type="text/javascript">
  324. //<![CDATA[
  325.     function t(ipt){
  326.         window.status=ipt;
  327.         return true;
  328.     }
  329.     t('test this');
  330. //]]>
  331. </script>
  332.  
  333. 38.2 return undefined
  334. <script type="text/javascript">
  335. //<![CDATA[
  336.     function t(t){
  337.         if(t==null)return;
  338.     }
  339.     document.write(t()); // undefined
  340. //]]>
  341. </script>
  342.  
  343. 37. window
  344.  
  345. 37. window.open
  346.  
  347. 37.1 打开没有焦点的窗口
  348. <script type="text/javascript">
  349. //<![CDATA[
  350.     var w=window.open('/')
  351.     w.blur();
  352.     window.focus();
  353. //]]>
  354. </script>
  355.  
  356. 36. confirm() 确认框
  357.  
  358. 36.1 带条件确认框
  359. <script type="text/javascript">
  360. //<![CDATA[
  361.     var x=confirm('what doing with you')
  362.         if(x){alert('ok')}else alert('good choice');
  363. //]]>
  364. </script>
  365.  
  366. 35. delete 
  367. <script type="text/javascript">
  368. //<![CDATA[
  369.     var t={i:'ok', j:'ok too'}
  370.     document.write(delete t.i); // true
  371.     document.write('<br/>');
  372.     document.write(delete t); // false 不能删除 使用 var 定义的变量或对象
  373.     document.write('<br/>');
  374.     document.write(delete n); // true 删除不存在的元素.
  375.     document.write('<br/>');
  376.     document.write(delete j); // true
  377.     document.write('<br/>');
  378. //]]>
  379. </script>
  380.  
  381. 34. Math 对象
  382.  
  383. 34.1 Math.random();
  384.  
  385. 34.1.1 生成 1 至 4 的随机数
  386. <script type="text/javascript">
  387. //<![CDATA[
  388.     document.write(parseInt(Math.random()*4+1)+'<br/>');
  389. //]]>
  390. </script>
  391.  
  392. 34.2 求 x 的 n 次方
  393. <script type="text/javascript">
  394. //<![CDATA[
  395.     document.write(Math.pow(2,10));
  396. //]]>
  397. </script>
  398.  
  399. 33. obj.valueOf() 返回原始值
  400. <script type="text/javascript">
  401. //<![CDATA[
  402.     var a=1, b='test', c=false, d=new Object();
  403.     document.write(a.valueOf()+'<br/>'); // 1
  404.     document.write(b.valueOf()+'<br/>'); // test
  405.     document.write(c.valueOf()+'<br/>'); // false
  406.     document.write(d.valueOf()+'<br/>'); // [object Object]
  407. //]]>
  408. </script>
  409.  
  410. 32. typeof 查看数据类型
  411. <script type="text/javascript">
  412. //<![CDATA[
  413.     var a=['a', 'b', 'c']
  414.     document.write(typeof a); // object
  415.     document.write(a.valueOf()); // a, b, c
  416.     document.write(typeof null); // object
  417. //]]>
  418. </script>
  419.  
  420. 32.1 
  421. <script type="text/javascript">
  422. //<![CDATA[
  423.     var a=['a', 'b', 'c']
  424.     document.write(typeof a); // object
  425.     document.write(a.valueOf()); // a, b, c
  426. //]]>
  427. </script>
  428.  
  429. 31. function 函数
  430.  
  431. 31.1 三种定义函数的方法
  432. <script type="text/javascript">
  433. //<![CDATA[
  434.     function f(){ alert('function f');};
  435.     var f1=function(){ alert('function f1');};
  436.     var f2=new Function('', "alert('function f2')");
  437.     f(); f1(); f2();
  438. //]]>
  439. </script>
  440.  
  441. 31.1.1 三种定义函数的方法 1
  442. <script type="text/javascript">
  443. //<![CDATA[
  444.     var t=new Function('a', 'b', 'return a+b;')
  445.     document.write(t('ok ', 'test this'));
  446. //]]>
  447. </script>
  448.  
  449. 31.1.1.1 三种定义函数的方法 1.1
  450. <script type="text/javascript">
  451. //<![CDATA[
  452.     var t=new Function("return 'test this'")
  453.     document.write(t());
  454. //]]>
  455. </script>
  456.  
  457. 31.1.2 三种定义函数的方法 2
  458. <script type="text/javascript">
  459. //<![CDATA[
  460.     var t=function(str){return 'this test'+str};
  461.         
  462.         document.write(t(', right.'));
  463. //]]>
  464. </script>
  465.  
  466. 31.2 在标签中写函数
  467. <div οnclick="javascript:var go=function(){alert(111)};go();">div 可用</div>
  468. <div οnclick="go()">div1 不可用</div>
  469. <div οnclick="javascript:( function(){alert(111);} )()">div2 可用</div>
  470. <div οnclick="javascript:function go(){alert(111);}go();">div3 可用</div>
  471.  
  472. 31.3 创建嵌套函数
  473. <script type="text/javascript">
  474. //<![CDATA[
  475.     function level1(){
  476.         var lNm='level 1'
  477.         document.write(lNm+'<br/>');
  478.         function level2(){
  479.             var lNm='level 2'
  480.             document.write(lNm+'<br/>');
  481.         }
  482.         level2();
  483.     }
  484.     level1();
  485. //]]>
  486. </script>
  487.  
  488. 31.4 创建递归函数
  489. <%
  490.     var i=0;
  491.     function t(){
  492.         i++;
  493.         if(i<100){
  494.             Response.Write(i+'<br/>');
  495.             return t();
  496.         }
  497.         Response.Write(i+'<br/>');
  498.     }
  499.     t();
  500. %>
  501.  
  502. 31.4.1 创建递归函数 1 
  503. <script type="text/javascript">
  504. //<![CDATA[
  505.     function fctr(n){
  506.         if(n<=1)return 1;
  507.         return n*fctr(n-1);
  508.     }
  509.     document.write(fctr(4));
  510. //]]>
  511. </script>
  512.  
  513. 31.5 创建 函数对象
  514. <script type="text/javascript">
  515. //<![CDATA[
  516.     function fT(){
  517.         this.title='o test';
  518.         this.subtitle=fTt();
  519.         function fTt(){
  520.             return 'o subtitle';
  521.         }
  522.     }
  523.     var t=new fT();
  524.         document.write(t.title+'<br/>');
  525.         document.write(t.subtitle+'<br/>');
  526.     t=null;
  527. //]]>
  528. </script>
  529.  
  530. 31.6 在函数内部定义的函数只能在该函数内部调用
  531. <script type="text/javascript">
  532. //<![CDATA[
  533.     function lev1(){
  534.         document.write('lev 1<br/>');
  535.         function lev2(){
  536.             document.write('lev 2<br/>');
  537.         }    
  538.         lev2();
  539.     }
  540.     lev1();
  541.     lev2(); // 出错, 在函数内部定义的函数只能在该函数内部调用
  542.     
  543. //]]>
  544. </script>
  545.  
  546. 31.7 定义函数变量, 并赋值
  547. <script type="text/javascript">
  548. //<![CDATA[
  549.     var t=(function(str){return str+str+'<br/>';})('test this ')
  550.     document.write(t);
  551. //]]>
  552. </script>
  553.  
  554. 31.8 变量引用函数
  555. <script type="text/javascript">
  556. //<![CDATA[
  557.     function t(x){
  558.         x+='';
  559.         return x+x;
  560.     }    
  561.     document.write(t('how fun ')+'<br>'); // how fun how fun 
  562.     var u=t // u 引用 函数 t
  563.     document.write(u('how fun too ')+'<br>'); // how fun too how fun too 
  564. //]]>
  565. </script>
  566.  
  567. 31.9 在对象中存储函数, 并引用
  568. <script type="text/javascript">
  569. //<![CDATA[
  570.     var o=new Object();
  571.         o.t=function(x){x+=''; return x+x;}
  572.     var t=o.t('test this ');
  573.     document.write(t);
  574. //]]>
  575. </script>
  576.  
  577. 31.10 在数组中存储函数, 并引用
  578. <script type="text/javascript">
  579. //<![CDATA[
  580.     var a=new Array(3);
  581.         a[0]=function(x){x+='';return x+x};
  582.         a[1]='test this ';
  583.         a[2]=a[0](a[1]);
  584.         document.write(a[2]);
  585. //]]>
  586. </script>
  587.  
  588. 31.11 function.call()
  589. <script type="text/javascript">
  590. //<![CDATA[
  591.     var o=new Object();
  592.     function f(a, b){
  593.         return a+b
  594.     }
  595.     document.write(f.call(o, 1, 2));
  596. //]]>
  597. </script>
  598.  
  599. 31.12 在函数内定义的变量可以被下级内嵌函数调用 
  600. <script type="text/javascript">
  601. //<![CDATA[
  602.     function t(){
  603.         var x=" this's test ";
  604.         function u(){
  605.             document.write(x.bold());
  606.         }
  607.         u();
  608.     }
  609.     t();
  610. //]]>
  611. </script>
  612.  
  613. 30. Date()
  614.  
  615. 30.1 输出年月日, 时分秒 的日期格式
  616. <script type="text/javascript">
  617. //<![CDATA[
  618.     var $dt=new Date();
  619.     document.write('yyyy-nn-dd: '+$dt.toLocaleDateString()+'<br/>');
  620.     document.write('hh-mm-ss: '+$dt.toLocaleTimeString());
  621. //]]>
  622. </script>
  623.  
  624. 29. isNaN
  625. <script type="text/javascript">
  626. //<![CDATA[
  627.     var $str='this is string';
  628.     var $num=12.3;
  629.     //alert(isNaN($str));
  630.     alert(isNaN($num));
  631. //]]>
  632. </script>
  633.  
  634. 28. new 语法
  635.   new Boolean(false)
  636.   new Number(0)
  637.   new String("")
  638.   new Array()
  639.   new Object()
  640.   new Date();
  641.   new Error();
  642.  
  643. 27. RegExp
  644.  
  645. 27.1 普通操作 1
  646. <script type="text/javascript">
  647. //<![CDATA[
  648.     var $str='this is a test'
  649.     with(document){
  650.         write('test str is: '+$str+'<br/>');
  651.         write('$str.search(/is a/i): '+$str.search(/is a/i)+'<p/>');
  652.         
  653.         write('$str.replace(/ {1,1}/ig,"--"): '+$str.replace(/ {1,1}/ig,"--")+'<p/>');
  654.         
  655.         write('$str.match(/t[/S]{1,3}/ig): '+$str.match(/t[/S]{1,3}/ig)+'<p/>');
  656.         
  657.         var $temp=$str.match(/(t[/S]{1,3})/ig)
  658.         for ($i=0; $i<$temp.length; $i++){
  659.             write($temp[$i]+'<br/>');
  660.         }
  661.     }
  662. //]]>
  663. </script>
  664.  
  665. 27.2 test 语句
  666. <script type="text/javascript">
  667. //<![CDATA[
  668.     var $pattern=/this/i;
  669.         alert($pattern.test('this is a test'));
  670. //]]>
  671. </script>
  672.  
  673. 27.3 exec 语句
  674. <script type="text/javascript">
  675. //<![CDATA[
  676.     var $pattern=/this/ig;
  677.     var $str='this is a test, this is repeat';
  678.     var $result;
  679.     while(($result=$pattern.exec($str))!=null){
  680.         alert($result[0]+'>>> index of str: '+$result.index+' $pattern lastIndex: '+$pattern.lastIndex);
  681.     }
  682. //]]>
  683. </script>
  684.  
  685. 26. Array()
  686.  
  687. 26.0 简单创建数组, 并赋值
  688. <script type="text/javascript">
  689. //<![CDATA[
  690.     var ar=new Array();
  691.         for(var i=0; i<10; i++){
  692.             ar[i]='array '+i
  693.             document.write(ar[i]+'<br/>');
  694.         }
  695. //]]>
  696. </script>
  697.  
  698. 26.0.1 简单创建数组, 并赋值 2
  699. <script type="text/javascript">
  700. //<![CDATA[
  701.     var ar=['v1', 'v2', 'v3', 'v4', 'v5']
  702.     for(var i=0; i<ar.length; i++){
  703.         document.write(ar[i]+'<br/>');
  704.     }
  705. //]]>
  706. </script>
  707.  
  708. 26.0.2 简单创建数组
  709. <script type="text/javascript">
  710. //<![CDATA[
  711.     var ar=new Array(10);
  712.         document.write(ar.length);
  713. //]]>
  714. </script>
  715.  
  716. 26.0.3 创建数组中的数组
  717. <script type="text/javascript">
  718. //<![CDATA[
  719.     var ar=new Array('v1', 'v2', 'v3', ['v41', 'v42']);
  720.     for(var i=0; i<ar.length; i++){
  721.         document.write(ar[i].toString()+'<br/>');
  722.     }
  723. //]]>
  724. </script>
  725.  
  726. 26.0.4 创建数组中的对象
  727. <script type="text/javascript">
  728. //<![CDATA[
  729.     var ar=new Array('v1', 'v2', 'v3', {x:'v41', y:'v42'});
  730.     for(var i=0; i<ar.length; i++){
  731.         if(typeof(ar[i])=='string')
  732.         document.write(ar[i]+'<br/>');
  733.         else if(typeof(ar[i])=='object'){
  734.             document.write('object<br/>');
  735.             for(var j in ar[i]){
  736.                 document.write('obj: '+j+' obj val '+ar[i][j]+'<br/>');
  737.             }
  738.         }
  739.     }
  740. //]]>
  741. </script>
  742.  
  743. 26.1 [array].push(), [array].pop()
  744. <script type="text/javascript">
  745. //<![CDATA[
  746.     var $array=new Array('a', 'b', 'c', 'd', 'e')
  747.     
  748.     with(document){
  749.         write('source $array.join: '+$array.join()+'<br/>');
  750.         write ($array.length+'<br/>');
  751.         $array.push(1,1,1,1,1,1,1,1,1,1);
  752.         write ('$array.push(1,1,1,1,1,1,1,1,1,1),  $array.length: '+$array.length+'<br/>');
  753.         write('join $array: '+$array.join()+'<p/>');
  754.         
  755.         write ($array.length+'<br/>');
  756.         $array.push(1, 2, 3, 4, 5);
  757.         write ('$array.push(1, 2, 3, 4, 5), $array.length: '+$array.length+'<br/>');
  758.         write('join $array: '+$array.join()+'<p/>');
  759.         
  760.         write('$array.pop(): '+$array.pop()+'<br/>');
  761.         write('$array.length & $array.join(): '+$array.length+' '+$array.join()+'<p/>');
  762.         
  763.         write('$array.pop(10): '+$array.pop(10)+'<br/>');
  764.         write('$array.length & $array.join(): '+$array.length+' '+$array.join()+'<br/>');
  765.     }
  766. //]]>
  767. </script>
  768.  
  769. 26.2 数组排序
  770. <script type="text/javascript">
  771. //<![CDATA[
  772.     var a=new Array();
  773.     for (var i=0, j=1; i<10;a[i]=j, i++, j++);
  774.     
  775.     document.write('<h2>未排序前:</h2>');
  776.     document.write(a.join());
  777.     
  778.     document.write('<h2>无条件排序:</h2>');
  779.     document.write(a.sort().join());
  780.     
  781.     temp=a.sort(function(a, b){return a-b});
  782.     document.write('<h2>a-b 排序:</h2>');
  783.     document.write(temp.join());
  784.     
  785.     temp=a.sort(function(a, b){return b-a});
  786.     document.write('<h2>b-a 排序:</h2>');
  787.     document.write(temp.join());
  788. //]]>
  789. </script>
  790.  
  791. 26.3 创建数组指定维数, 从 1 开始.
  792. <script type="text/javascript">
  793. //<![CDATA[
  794.     var a=new Array(3);
  795.     document.write((a.length+'').bold()+'<br/>');
  796.     for(var i=0; i<a.length; i++){
  797.         document.write(i+'<br/>');
  798.     }
  799. //]]>
  800. </script>
  801.  
  802. 25. toString
  803.  
  804. 25.1 使用 toString 列出数组中的元素
  805. <script type="text/javascript">
  806. //<![CDATA[
  807.     var $array=new Array('a', 'a1', 'a2');
  808.     alert($array.toString());
  809. //]]>
  810. </script>
  811.  
  812. 24. object
  813.  
  814. 24.1 创建简单对象
  815. <script type="text/javascript">
  816. //<![CDATA[
  817.     var $obj=new Object();
  818.     $obj={$title:"object example", $date:Date()}
  819.     alert($obj.$date);
  820. //]]>
  821. </script>
  822.  
  823. 24.1.1 创建简单对象 一
  824. <script type="text/javascript">
  825. //<![CDATA[
  826.     var cnt=new Object();
  827.         cnt.title='title this';
  828.         cnt.content='content this'
  829.         cnt.date='the publishing date of '+Date();
  830.     
  831.     document.write('title: '+cnt.title+'<br/>cnt.content: '+cnt.content+'<br/>cnt.date: '+cnt.date);
  832. //]]>
  833. </script>
  834.  
  835. 24.1.2 创建简单对象 二
  836. <script type="text/javascript">
  837. //<![CDATA[
  838.     var cnt={
  839.         title:'title this',
  840.         content:'content this',
  841.         date:'the publishing date of '+Date()
  842.     }
  843.         document.write('title: '+cnt.title+'<br/>cnt.content: '+cnt.content+'<br/>cnt.date: '+cnt.date);
  844. //]]>
  845. </script>
  846.  
  847. 24.2 创建嵌套对象
  848. <script type="text/javascript">
  849. //<![CDATA[
  850.     var cnt={
  851.         title:{
  852.             headertitle:'header title',
  853.             subtitle:'subtitle this'
  854.         },
  855.         content:'content this',
  856.         date:'the publishing date of '+new Date().toLocaleString()
  857.     }
  858.         document.write('title: '+cnt.title.headertitle.bold().fontcolor('red')+
  859.         '<br/>cnt.title.subtitle: '+cnt.title.subtitle.fontcolor('blue')+
  860.         '<br/>cnt.content: '+cnt.content.fontcolor('goldenrod')+
  861.         '<br/>cnt.date: '+cnt.date);
  862. //]]>
  863. </script>
  864.  
  865. 24.2.1 创建嵌套对象 1
  866. <script type="text/javascript">
  867. //<![CDATA[
  868.     var o=new Object();
  869.         o.title=' this title ';
  870.         o.content=' this content';
  871.         o.lve=new Object();
  872.         o.lve.title=' this o.lve title';
  873.         o.lve.content=' this o.lve content';
  874.         
  875.         document.write('o.title: '+o.title.bold()+'<br/>');
  876.         document.write('o.content: '+o.content.bold()+'<br/>');
  877.         document.write('o.lve.title: '+o.lve.title.bold()+'<br/>');
  878.         document.write('o.lve.content: '+o.lve.content.bold()+'<br/>');
  879. //]]>
  880. </script>
  881.  
  882. 24.3 删除对象中的属性
  883. <script type="text/javascript">
  884. //<![CDATA[
  885.     var $obj=new Object();
  886.     $obj={$title:"object example", $date:Date()}
  887.     //alert($obj.$date);
  888.     
  889.     var $name="";
  890.     for(name in $obj)$name+=name+'/n';
  891.     alert($name);
  892.     
  893.     delete $obj.$date
  894.     
  895.     var $name="";
  896.     for(name in $obj)$name+=name+'/n';
  897.     alert($name);
  898. //]]>
  899. </script>
  900.  
  901. 24.4 遍历对象中的属性名
  902. <script type="text/javascript">
  903. //<![CDATA[
  904.     var $obj=new Object();
  905.     $obj={$title:"object example", $date:Date()}
  906.     //alert($obj.$date);
  907.     
  908.     var $name="";
  909.     for(name in $obj)$name+=name+'/n';
  910.     alert($name);
  911. //]]>
  912. </script>
  913.  
  914. 24.4.1 遍历对象中的属性名 1
  915. <script type="text/javascript">
  916. //<![CDATA[
  917.     var o=new Object();
  918.         o.title=' this title ';
  919.         o.content=' this content';
  920.         o.lve=new Object();
  921.         o.lve.title=' this o.lve title';
  922.         o.lve.content=' this o.lve content';
  923.         
  924.         for (var i in o){
  925.             if(typeof o[i]!=='object'){
  926.                 document.write('object o, element: '+i.fontcolor('red')+' value: '+o[i].bold()+'<br/>');
  927.             } else {
  928.                 document.write('object '+i+' element<br/>');
  929.                 for(var j in o[i]){
  930.                     document.write('subobject element :'+j.fontcolor('red')+
  931.                     ' subobject value: '+o[i][j].bold()+'<br/>');
  932.                 }
  933.             }
  934.         }
  935. //]]>
  936. </script>
  937.  
  938. 24.5 obj.propertyIsEnumerable('ele')
  939. <script type="text/javascript">
  940. //<![CDATA[
  941.     var d=new Date();
  942.         d.x='test';
  943.     var s=new String();
  944.     var o=new Object();
  945.     var f=new Function('thest')
  946.     
  947.     document.write(d.propertyIsEnumerable('x'));
  948.     document.write(d.propertyIsEnumerable('y'));
  949. //]]>
  950. </script>
  951.  
  952. 24.6 obj.hasOwnProperty('ele')
  953. <script type="text/javascript">
  954. //<![CDATA[
  955.     var d=new Date();
  956.         d.x='test';
  957.     var s=new String();
  958.     var o=new Object();
  959.     var f=new Function('thest')
  960.     
  961.     document.write(d.hasOwnProperty('x'));
  962.     document.write(d.hasOwnProperty('y'));
  963. //]]>
  964. </script>
  965.  
  966. 23. call, apply
  967.  
  968. 23.1 apply
  969.  
  970. 23.1.1 找出数组中最大的数
  971. <script type="text/javascript">
  972. //<![CDATA[
  973.     $array=new Array(2, 9, 12, 33, 2, 4444, 22)
  974.     var $temp=Math.max.apply(null, $array)
  975.     alert($temp)
  976. //]]
  977. </script>
  978.  
  979. 22. break, continue
  980.  
  981. 22.1 continue
  982. <script type="text/javascript">
  983. //<![CDATA[
  984.     for (var $i=0; $i<10; $i++){
  985.         if($i==5){continue;}
  986.         document.write ($i+'<br/>')
  987.     }
  988. //]]>
  989. </script>
  990.  
  991. 22.2 break 
  992.  
  993. 22.2.1 break 标签
  994. <script type="text/javascript">
  995. //<![CDATA[
  996. //document.write('<xmp>');
  997. outerloop:
  998.     for (var i=0; i<10; i++){
  999.         document.writeln(('outerloop:'+i).bold()+'<br/>')
  1000.         innerloop:
  1001.         for(var j=0; j<10; j++){;
  1002.             if(i==5)break outerloop;
  1003.             if(j==9)break innerloop;
  1004.             document.writeln(('innerloop: '+j).fontcolor('blue')+'<br/>');
  1005.         }
  1006.     }
  1007. //document.write('</xmp>');
  1008. //]]>
  1009. </script>
  1010.  
  1011. 21. with
  1012.  
  1013. 21.1 简单 with
  1014. <script type="text/javascript">
  1015. //<![CDATA[
  1016.     with(document){
  1017.         write('ok');
  1018.     }    
  1019. //]]>
  1020. </script>
  1021.  
  1022. 21.1 简单with (另一写法)
  1023.     var $dw=document
  1024.     $dw.write('ok this a test');
  1025.  
  1026. 20. try, catch, finally
  1027.  
  1028. 20.1 简单 try, catch
  1029.     try{
  1030.         5*kkk
  1031.     }
  1032.     catch(e){
  1033.         alert(e)
  1034.     }
  1035.     
  1036. 20.1.1 简单 try, catch 1
  1037. <script type="text/javascript">
  1038. //<![CDATA[
  1039.     document.write('<h2>window object:</h2>');
  1040.     for(var i in window){
  1041.         try{
  1042.             document.write(i.bold()+' '+(window[i]+'').fontcolor('blue')+'<br/>');
  1043.         } catch (e) {
  1044.             document.write(i.fontcolor('red').bold()+': ');
  1045.             document.write(e.description.fontcolor('red')+'<br/>');
  1046.         }
  1047.     }
  1048. //]]>
  1049. </script>
  1050.  
  1051. 19. for in
  1052.  
  1053. 19.1 简单 for in
  1054. <script type="text/javascript">
  1055. //<![CDATA[
  1056. /*     document.write('<h2>top object:</h2>');
  1057.     for(var i in top){
  1058.         try{
  1059.             document.write(i.bold()+' '+(top[i]+'').fontcolor('blue')+'<br/>');
  1060.         } catch (e) {
  1061.             document.write(i.fontcolor('red').bold()+': ');
  1062.             document.write(e.description.fontcolor('red')+'<br/>');
  1063.         }
  1064.     }
  1065.     
  1066.     document.write('<h2>parent object:</h2>');
  1067.     for(var i in parent){
  1068.         try{
  1069.             document.write(i.bold()+' '+(parent[i]+'').fontcolor('blue')+'<br/>');
  1070.         } catch (e) {
  1071.             document.write(i.fontcolor('red').bold()+': ');
  1072.             document.write(e.description.fontcolor('red')+'<br/>');
  1073.         }
  1074.     }
  1075.     
  1076.     document.write('<h2>screen object:</h2>');
  1077.     for(var i in screen){
  1078.         try{
  1079.             document.write(i.bold()+' '+(screen[i]+'').fontcolor('blue')+'<br/>');
  1080.         } catch (e) {
  1081.             document.write(i.fontcolor('red').bold()+': ');
  1082.             document.write(e.description.fontcolor('red')+'<br/>');
  1083.         }
  1084.     }
  1085.      */
  1086.     try{
  1087.         document.write('<h2>clipboardData object:</h2>');
  1088.         for(var i in clipboardData){
  1089.             try{
  1090.                 document.write(i.bold()+' '+(clipboardData[i]+'').fontcolor('blue')+'<br/>');
  1091.             } catch (e) {
  1092.                 document.write(i.fontcolor('red').bold()+': ');
  1093.                 document.write(e.description.fontcolor('red')+'<br/>');
  1094.             }
  1095.         }
  1096.     } catch(ex) {
  1097.         document.write((ex+'').fontcolor('red'));
  1098.     }
  1099.     
  1100.     document.write('<h2>window object:</h2>');
  1101.     for(var i in window){
  1102.         try{
  1103.             document.write(i.bold()+' '+(window[i]+'').fontcolor('blue')+'<br/>');
  1104.         } catch (e) {
  1105.             document.write(i.fontcolor('red').bold()+': ');
  1106.             document.write(e.description.fontcolor('red')+'<br/>');
  1107.         }
  1108.     }
  1109. /*     
  1110.     document.write('<h2>Option object:</h2>');
  1111.     for(var i in Option){
  1112.         try{
  1113.             document.write(i.bold()+' '+(Option[i]+'').fontcolor('blue')+'<br/>');
  1114.         } catch (e) {
  1115.             document.write(i.fontcolor('red').bold()+': ');
  1116.             document.write(e.description.fontcolor('red')+'<br/>');
  1117.         }
  1118.     }
  1119.  */    
  1120.     document.write('<h2>frames object:</h2>');
  1121.     for(var i in frames){
  1122.         try{
  1123.             document.write(i.bold()+' '+(frames[i]+'').fontcolor('blue')+'<br/>');
  1124.         } catch (e) {
  1125.             document.write(i.fontcolor('red').bold()+': ');
  1126.             document.write(e.description.fontcolor('red')+'<br/>');
  1127.         }
  1128.     }
  1129. /*     
  1130.     document.write('<h2>self object:</h2>');
  1131.     for(var i in self){
  1132.         try{
  1133.             document.write(i.bold()+' '+(self[i]+'').fontcolor('blue')+'<br/>');
  1134.         } catch (e) {
  1135.             document.write(i.fontcolor('red').bold()+': ');
  1136.             document.write(e.description.fontcolor('red')+'<br/>');
  1137.         }
  1138.     }
  1139.  */
  1140.     document.write('<h2>document object:</h2>');
  1141.     for(var i in document){
  1142.         document.write(i.bold()+' '+(document[i]+'').fontcolor('blue')+'<br/>');
  1143.     }
  1144.     
  1145.     document.write('<h2>history object:</h2>');
  1146.     for(var i in history){
  1147.         try{
  1148.             document.write(i.bold()+' '+(history[i]+'').fontcolor('blue')+'<br/>');
  1149.         } catch (e) {
  1150.             document.write(i.fontcolor('red').bold()+': ');
  1151.             document.write(e.description.fontcolor('red')+'<br/>');
  1152.         }
  1153.     }
  1154. /*     
  1155.     document.write('<h2>Image object:</h2>');
  1156.     for(var i in Image){
  1157.         try{
  1158.             document.write(i.bold()+' '+(Image[i]+'').fontcolor('blue')+'<br/>');
  1159.         } catch (e) {
  1160.             document.write(i.fontcolor('red').bold()+': ');
  1161.             document.write(e.description.fontcolor('red')+'<br/>');
  1162.         }
  1163.     }
  1164.  */    
  1165.     document.write('<h2>navigator object:</h2>');
  1166.     for(var i in navigator){
  1167.         try{
  1168.             document.write(i.bold()+' '+(navigator[i]+'').fontcolor('blue')+'<br/>');
  1169.         } catch (e) {
  1170.             document.write(i.fontcolor('red').bold()+': ');
  1171.             document.write(e.description.fontcolor('red')+'<br/>');
  1172.         }
  1173.     }
  1174.     
  1175.     document.write('<h2>location object:</h2>');
  1176.     for(var i in location){
  1177.         document.write(i.bold()+' '+(location[i]+'').fontcolor('blue')+'<br/>');
  1178.     }
  1179. //]]>
  1180. </script>
  1181.  
  1182. 19.1.1 简单 for in 1
  1183.     for (var $i in window){
  1184.         document.write($i+'<br/>');
  1185.     }
  1186.     
  1187. 19.2 for in 赋值 [数组]
  1188.     var $array=new Array();
  1189.     var i=0;
  1190.     for ($array[i++] in window){
  1191.         //document.write($i+'<br/>');
  1192.     }
  1193.     alert($array[0]);
  1194.     
  1195. 19.2.1 for in 赋值 [数组] 1
  1196. <script type="text/javascript">
  1197. //<![CDATA[
  1198.     var o={a:'aaa', b:'bbb', c:'ccc'};
  1199.     var a=new Array();
  1200.     var i=0;
  1201.     for(a[i++] in o)document.write(a[i-1]);
  1202.     //for(i in a)document.write(a[i]);
  1203. //]]>
  1204. </script>
  1205.  
  1206. 18. for
  1207.  
  1208. 18.1 简单 for
  1209.     for (var $i=0; $i<10; $i++) {
  1210.         document.write($i)
  1211.     }
  1212.  
  1213. 18.2 简单 for 1
  1214.     for (var $i=0, $j=10; $i<10; $i++, $j--) {
  1215.         document.write(($i*$j)+'<br/>')
  1216.     }
  1217.  
  1218. 18.3 循环列出从 a - z 的字母
  1219. <script type="text/javascript">
  1220. //<![CDATA[
  1221.     for(var $i='a'.charCodeAt(); $i<='z'.charCodeAt(); $i++){
  1222.         document.write(String.fromCharCode($i)+'<br/>')
  1223.     }
  1224. //]]>
  1225. </script>
  1226.  
  1227. 18.3 n阶乘
  1228. <script type="text/javascript">
  1229. //<![CDATA[
  1230.     var $fN=10
  1231.     for (var $i=1, $j=1; $i<=$fN; $i++, $j*=$i){
  1232.         if($i==$fN){document.write($j);}
  1233.     }
  1234. //]]>
  1235. </script>
  1236.  
  1237. 18.4 Fibonacci Numbers
  1238. <script type="text/javascript">
  1239. //<![CDATA[
  1240.     for(var $i=0, $j=1, $k=0, $fbcc=0; $i<50; $i++){
  1241.         document.write($i+'='+$fbcc+'<br/>');
  1242.          $fbcc=$j+$k;
  1243.          $j=$k;
  1244.          $k=$fbcc;
  1245.     }
  1246. //]]>
  1247. </script>
  1248.  
  1249. 17. do while
  1250.  
  1251. 17.1 简单 do while
  1252.     var $count=0
  1253.     do{
  1254.         document.write($count)
  1255.     } while(++$count<10)
  1256.     
  1257. 17.1.2 简单 do while
  1258.     var $count=1
  1259.     do{
  1260.         document.write($count)
  1261.     } while($count++<10)
  1262.  
  1263. 16. while
  1264.  
  1265. 16. 简单 while
  1266.     var $count=0
  1267.     while($count<10){ document.write($count);$count++;}
  1268.  
  1269. 15 if 判断
  1270.  
  1271. 15.1 简单 if 判断
  1272.     function ubbTag(fId, str){
  1273.             document.getElementById(fId).content.focus();
  1274.         var strEnd=str.replace(//[/ig,'[/')
  1275.         if (strEnd.indexOf('=')>-1){ strEnd=strEnd.replace(/(.*?)/=.*?/]/,'$1]') }
  1276.         if((document.selection)&&(document.selection.type== "Text")){
  1277.             var oStr=document.selection.createRange();
  1278.             oStr.text=str+oStr.text+strEnd
  1279.         } else {
  1280.             document.getElementById(fId).content.value+=str+strEnd
  1281.         }
  1282.     }
  1283.  
  1284. 15.1.1 简单 if 判断 2
  1285. <script type="text/javascript">
  1286. //<![CDATA[
  1287.     var i=0, j=1;
  1288.     i>j?j+=i:i+=j;
  1289.     document.write(i+' '+j);
  1290. //]]>
  1291. </script>
  1292.  
  1293. 15.1.2 简单 if 判断 3
  1294. <script type="text/javascript">
  1295. //<![CDATA[
  1296.     if(!cdti||cdti=='')var cdti='test this';
  1297.     document.write(cdti);
  1298. //]]>
  1299. </script>
  1300.     
  1301. 15.2 if 判断的 三个 写法
  1302. <script type="text/javascript">
  1303. //<![CDATA[
  1304.     var a=b=5, c=6
  1305.     if(a==b)document.write('a equal b <br/>');
  1306.     (a==b)&&document.write('a equal b <br/>');
  1307.     a==b?document.write('a equal b <br/>'):'';
  1308. //]]>
  1309. </script>
  1310.  
  1311. 15.3 判断执行文件使用网络还是使用本地协议
  1312. <script type="text/javascript">
  1313. //<![CDATA[
  1314.     if(location.protocol=='file:'){
  1315.         alert('local')
  1316.     } else alert('internet');
  1317. //]]>
  1318. </script>
  1319.  
  1320. 15.4 if...else 写法
  1321. <script type="text/javascript">
  1322. //<![CDATA[
  1323.     //var i=0
  1324.     var i=1
  1325.     if(i==0)document.write('i=0');
  1326.     else document.write('i!=0');
  1327. //]]>
  1328. </script>
  1329.  
  1330. 14. switch
  1331.  
  1332. 14.1 简单 switch
  1333. /*         switch(strEnd){
  1334.             case '/[//html]' :strEnd=strEnd.replace(///[//,'HTML 和 JS 代码支持[');break;
  1335.             case '/[//code]' :strEnd=strEnd.replace(///[//,'代码[');break;
  1336.             case '/[//quote]' :strEnd=strEnd.replace(///[//,'引用[');break;
  1337.             case '/[//cite]' :strEnd=strEnd.replace(///[//,'引用[');break;
  1338.             case '/[//linenum]' :strEnd=strEnd.replace(///[//,'显示行号[');break;
  1339.             case '/[//b]' :strEnd=strEnd.replace(///[//,'粗体[');break;
  1340.             case '/[//i]' :strEnd=strEnd.replace(///[//,'叙体[');break;
  1341.             case '/[//u]' :strEnd=strEnd.replace(///[//,'下划线[');break;
  1342.             case '/[//flash]' :strEnd=strEnd.replace(///[//,'Flash 动画[');break;
  1343.             case '/[//sound]' :strEnd=strEnd.replace(///[//,'背景声音[');break;
  1344.             case '/[//mms]' :strEnd=strEnd.replace(///[//,'WM格式流数据[');break;
  1345.             case '/[//rtsp]' :strEnd=strEnd.replace(///[//,'Real格式流数据[');break;
  1346.             case '/[//ra]' :strEnd=strEnd.replace(///[//,'在线Real Player播放音频文件[');break;
  1347.             case '/[//real]' :strEnd=strEnd.replace(///[//,'Real Player 播放视频文件[');break;
  1348.             case '/[//wm]' :strEnd=strEnd.replace(///[//,'在线Windows Media Player播放视频文件[');break;
  1349.             case '/[//wma]' :strEnd=strEnd.replace(///[//,'在线Windows Media Player播放音频文件[');break;
  1350.             case '/[//iframe]' :strEnd=strEnd.replace(///[//,'插入网页[');break;
  1351.         } */
  1352.  
  1353. 13. 时间操作
  1354.  
  1355. 13.1 以毫秒为单位的倒计时跳转, 来源 CSDN
  1356. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  1357. <script type="text/javascript">
  1358. //<![CDATA[
  1359.     var go=20000;
  1360.     var timer=null;
  1361.     var endTime = new Date().getTime() + go ;
  1362.     function interval()
  1363.     {
  1364.         var n=(endTime-new Date().getTime())/1000;
  1365.         if(n<0) return;
  1366.         document.getElementById("jumpTo").innerHTML = n.toFixed(3);
  1367.         setTimeout(interval, 10);
  1368.     }
  1369.     window.οnlοad=function(){
  1370.         timer=setTimeout("window.location.href='/'", go);
  1371.         interval();
  1372.     }
  1373. //]]>
  1374. </script>
  1375. <span id="jumpTo">20.000</span> 秒后 将自动</span>跳转到
  1376.  
  1377. 12. 随机数
  1378.  
  1379. 12.1 生成 GUID
  1380. <!--start 12.1-->
  1381. <html>
  1382. <!-- DW6 -->
  1383. <head>
  1384. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  1385. <title>Untitled Document</title>
  1386. <script>
  1387. function fGuid()
  1388.     {
  1389.     var g ="";
  1390.     for(var i = 0; i < 32; i++)
  1391.     g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "");
  1392.     alert(g.toUpperCase());
  1393. }
  1394. </script>
  1395. </head>
  1396. <body onLoad="fGuid();">
  1397. </body>
  1398. </html>
  1399. <!--end 12.1-->
  1400.  
  1401. 12.1.1 生成 GUID 2
  1402. <!--start 12.1.1 -->
  1403. <html>
  1404. <!-- DW6 -->
  1405. <head>
  1406. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  1407. <title>Untitled Document</title>
  1408. <script>
  1409. function fGuid() {
  1410.     var g ="";
  1411.     for(var i = 0; i < 32; i++)
  1412.     g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "");
  1413.     //alert(g.toUpperCase());
  1414.     return g.toUpperCase();
  1415. }
  1416. </script>
  1417. </head>
  1418. <body>
  1419. <form name="form1" method="post" action="">
  1420.   <input name="textfield" type="text" onMouseMove="this.value=fGuid();" size="50">
  1421. </form>
  1422. </body>
  1423. </html>
  1424. <!--end 12.1.1 -->
  1425.  
  1426. 11.1 检测字符串中是否出现某字符
  1427. <script>
  1428.     function test(str) {
  1429.             if(str.indexOf("|")>0) {
  1430.                 alert("have |")
  1431.             }
  1432.         }
  1433. </script>
  1434. <input type="text" value="kklskldflksd|ksdkfksjd" onMouseOver="test(this.value)" />
  1435.  
  1436. 11. 字符串
  1437.  
  1438. 10.2 鼠标移过选择表单项
  1439. onMouseOver="focus();select();" 
  1440.  
  1441. 10.1 单击鼠标右击关闭窗口 (我用左手)
  1442. <html οnmοusedοwn='window.close();'>
  1443.  
  1444. 10. 接收鼠标事件
  1445.  
  1446. 9. 正则表达式
  1447. function rbr() {
  1448.     re=//<br///>*/ig
  1449.     document.all.textarea.value=document.all.textarea.value.replace(re,"")
  1450.     }
  1451.  
  1452. 8. 顯示頁面所有元素
  1453. <script language="JavaScript" type="text/javascript">
  1454. //<![CDATA[
  1455.     for(var i in document) {
  1456.             document.write(i +"="+ document[i]+"<br/>")
  1457.         }
  1458. //]]>
  1459. </script>
  1460.  
  1461. 7. 文本框回车链接
  1462. <script>
  1463. <!-- 
  1464.     function ckKeyPress(jumpto){
  1465.         if(window.event.keyCode==13) {
  1466.             window.location.href="?"+jumpto
  1467.         }
  1468.     } 
  1469. //-->shawl.qiu script
  1470. </script>
  1471.  转到:<input type='text' οnmοusemοve='this.focus();this.select();' οnkeypress="ckKeyPress(this.value);" />
  1472.  
  1473. 6. IE 全屏显示
  1474. <script language="JavaScript">
  1475. <!--
  1476.     self.moveTo(0,0)
  1477.     self.resizeTo(screen.availWidth,screen.availHeight)
  1478. //-->
  1479. </script>
  1480.  
  1481. 5. 双击拷贝文本
  1482. <script LANGUAGE="JAVASCRIPT">
  1483. function ClipBoard(tdObj) { //IE Only
  1484.         var holdtext = document.all['holdtext'];
  1485.         holdtext.innerText = tdObj.innerText;
  1486.         Copied = holdtext.createTextRange();
  1487.         Copied.execCommand("Copy");
  1488.         alert("text copied");
  1489.     }
  1490. </script>
  1491. <form NAME="form1" METHOD="post" ACTION="">
  1492.         <textarea id="holdtext" style="display:none" ROWS="10"></textarea>
  1493. </form>
  1494.  
  1495. 4. 控制图片大小
  1496. <img src="images/teach/opera/01.jpg" οnlοad="javascript:if(this.width>500)this.width=500;" >
  1497. //如果图片宽度大于 500px, 则 更改宽度为 500px
  1498.  
  1499. 3.1 Javascript 结合 Asp 使用数组
  1500. <%    '连接数据库
  1501.     set rs=server.CreateObject("adodb.recordset") '创建 rs 数据查询
  1502.         rs.open "SELECT  a.*, b.* FROM ctglossarysubcat AS a INNER JOIN ctglossarysupercat AS b ON a.gcid = b.gcid order by a.gcid",MM_conn_string,1
  1503. %>
  1504. <script LANGUAGE="JAVASCRIPT">
  1505.         var onecount; //定义子类计数
  1506.         onecount=0; //设置子类计数默认值为0
  1507.         subcat = new Array(); //定义显示子类数组
  1508. <%        count = 0
  1509.         do while not rs.eof  %>
  1510.             subcat[<%=count%>] = new Array("<%=rs("gscat")%>","<%=rs("a.gcid")%>","<%=rs("gscid")%>"); 
  1511. <%            count = count + 1 
  1512.             rs.movenext
  1513.         loop 
  1514. %>
  1515.             onecount=<%=count%>; //子类条目总数
  1516. </script>
  1517. <%
  1518.         rs.close '关闭 rs 连接
  1519.     set rs=nothing 
  1520. %>
  1521.  
  1522. 3. 数组/Array
  1523.  
  1524. 2.2 实时显示更改的图片链接
  1525.  <input type="text" name="" value="<%response.write siteicon %>" οnkeyup="javascript:document.all.siteicon.src=(this.value);" /> <img src="<%response.write siteicon %>" alt="<%response.write sitename %>" id="siteicon" />
  1526.  
  1527. 2.1 按任何键关闭窗口 
  1528. <body onKeyPress="self.close()">
  1529.  
  1530. 2. 接收键盘事件
  1531.  
  1532. 1.6 鼠标经过自动提交表单
  1533. onMouseOut="submit()"
  1534.  
  1535. 1.5.1 鼠标经过时选中,并隐藏原框的文字, 移开再显示文字 ——文本框
  1536. <input NAME="findtext" TYPE="text" ID="findtext" value="查找" onMouseOver="this.focus();this.value=''" onMouseOut="this.value='查找';this.select()">
  1537. <!-- shawl.qiu script -->
  1538.  
  1539.  
  1540. 1.5 鼠标经过时选中 ——复选框 Focus()
  1541. <input type="checkbox" onMouseOver="checked='true'" />
  1542. <!-- shawl.qiu script -->
  1543.  
  1544. 1.4 Prompt
  1545. function prompter()
  1546.     {
  1547.         
  1548.         urlLink=window.prompt("请输入链接"," http://")
  1549.         text = window.prompt("链接名")
  1550.         
  1551.         if(text!="")
  1552.         document.formT.content.value+="[url="+urlLink+"]"+text+"[/url]"
  1553.     }
  1554.  
  1555.  
  1556. 1.3 最原始也最实用的  UBB 代码输入方法——SCRIPT & FORM
  1557. <form name="formT" id="formT" method="post" action="">
  1558.   <input type="button" value="url" οnclick="this.form.content.value+=''"/>
  1559.   <input type="button" value="code" οnclick="this.form.content.value+='

    CODE:'" />

  1560.   <input type="button" value="quote" οnclick="this.form.content.value+='

    QUOTE:

    '"/>
  1561.   <br />
  1562.   <textarea name="content" cols="60" rows="10"></textarea>
  1563.   <br />
  1564.   <input type="submit" name="Submit" value="Submit" />
  1565.   <input type="reset" name="Reset" value="Reset" />
  1566. </form>
  1567. <!-- shawl.qiu script -->
  1568.  
  1569. 1.2 确认按钮
  1570. <script LANGUAGE="JavaScript">
  1571. <!--//确认框, 注意表单名; 按钮属性, 按钮名字不能为 submit
  1572.     function confirmer()
  1573.         {
  1574.             if (confirm("现在提交?")) {
  1575.                 document.formT.submit()
  1576.                 }
  1577.             else {
  1578.                 document.formT.reset()
  1579.                 }
  1580.         }gggg
  1581. //Script By Shawl.qiu
  1582. //-->
  1583. </script>
  1584. <form name="formT" method="post" action="t1.asp">
  1585.     <input type="text" name="textfield" value="确认后提交">
  1586.     <input type="button" name="confbuton" value="提交" οnclick="confirmer();">
  1587. </form>
  1588.  
  1589.  
  1590. 1.1 前进后退按钮
  1591. <input TYPE="button" VALUE="后退" ONCLICK="history.back(-1)">
  1592. <input TYPE="button" VALUE="前进" ONCLICK="history.back(+1)">
  1593.  
  1594. 1. 按钮
  1595.  
  1596. 0. 语法
  1597.  
  1598. 0.1 Switch
  1599. function kP() {
  1600.     switch(event.keyCode) {
  1601.         case 27:    //按 ESC 键关闭窗口.
  1602.             window.close()
  1603.             break;    
  1604.         //case 120: //F9 转到主页
  1605.             //window.location.href="/"
  1606.             //break;
  1607.         default:
  1608.         break;
  1609.     }
  1610. }
  1611.  
  1612. -1. 运算符
  1613.  
  1614. -1.1 -- 与 ++
  1615. <script type="text/javascript">
  1616. //<![CDATA[
  1617.     var x;
  1618.     x=0; 
  1619.     document.write('x=0, x++: '+(x++));  // 执行 x 后, x 再加 1
  1620.     x=0; 
  1621.     document.write('<br/>x=0, ++x: '+(++x)); // 执行 x 前, x 先加 1
  1622.     x=2; 
  1623.     document.write('<br/>x=2, x--: '+(x--)); // 执行 x 后, x 再减 1
  1624.     x=2;
  1625.     document.write('<br/>x=2, --x: '+(--x)); // 执行 x 前, x 先减 1
  1626. //]]>
  1627. </script>
  1628.  
  1629. -1.1.1
  1630. <script type="text/javascript">
  1631. //<![CDATA[
  1632.     var i=1;
  1633.         j=++i;
  1634.         document.write(i);  //2
  1635.         document.write('<br/>');
  1636.         document.write(j); //2
  1637.         document.write('<br/>');
  1638.         
  1639.     var k=1
  1640.         l=k++
  1641.         document.write(k);  //2
  1642.         document.write('<br/>');
  1643.         document.write(l); //1
  1644. //]]>
  1645. </script>
  1646.  
  1647. -1.2 类型转换
  1648. <script type="text/javascript">
  1649. //<![CDATA[
  1650.     document.write('100'+100); //'100100' 字符串与数字相加
  1651.     document.write('<br/>');
  1652.     document.write('100'-0); //100 转字符串为数值
  1653.     document.write('<br/>');
  1654.     document.write(100+''); //'100' 数值转字符串
  1655.     document.write('<br/>');
  1656.     document.write(!!100); //true 转布尔值
  1657.     document.write('<br/>');
  1658.     document.write(!100); //false 转布尔值
  1659.     document.write('<br/>');
  1660. //]]>
  1661. </script>
  1662.  
  1663. -1.3 0/0=?
  1664. <script type="text/javascript">
  1665. //<![CDATA[
  1666.     document.write(0/0); //NaN
  1667. //]]>
  1668. </script>
  1669.  
  1670. -1.4 == 与 ===
  1671.  
  1672. -1.4.1 NaN
  1673. <script type="text/javascript">
  1674. //<![CDATA[
  1675.     document.write(NaN==NaN); //false
  1676.     document.write(NaN===NaN); //false
  1677. //]]>
  1678. </script>
  1679.  
  1680. -1.4.2 true 等价 1, false 等价 0.
  1681. <script type="text/javascript">
  1682. //<![CDATA[
  1683.     document.write(1==true); //true
  1684.     document.write('<br/>');
  1685.     document.write(0==false); //true
  1686.     document.write('<br/>');
  1687. //]]>
  1688. </script>
  1689.  
  1690. -1.5 in 操作符
  1691. <script type="text/javascript">
  1692. //<![CDATA[
  1693.     var o={e:'ok', f:'ok'}
  1694.         document.write('e' in o); // true
  1695.         document.write('<br/>');
  1696.         document.write('f' in o); // true
  1697.         document.write('<br/>');
  1698.         document.write('j' in o); // false
  1699.         document.write('<br/>');
  1700.         document.write('toString' in o); // false
  1701.         document.write('<br/>');
  1702. //]]>
  1703. </script>
  1704.  
  1705. -2. var 定义变量
  1706.  
  1707. -2.1 普通定义变量
  1708. <script type="text/javascript">
  1709. //<![CDATA[
  1710.     var i
  1711.         document.write(i+'<br/>');
  1712.     var j=1, k=2
  1713.         document.write(j+k+'<br/>');
  1714.     for(var l=0; l<10; l++)document.write(l+' ');;
  1715. //]]>
  1716. </script>
  1717.  
  1718. -2.1.1 普通定义变量 1
  1719. <script type="text/javascript">
  1720. //<![CDATA[
  1721.     var o={a:'aaa', b:'bbb', c:'ccc'}
  1722.     for(var i in o){
  1723.         document.write('object element name: '+i.bold()+' object value: '+o[i].fontcolor('blue')+'<br/>');
  1724.     }    
  1725. //]]>
  1726. </script>
  1727.  
  1728. -2.1.2 普通定义变量 2
  1729. <script type="text/javascript">
  1730. //<![CDATA[
  1731.     var i=j=k=l='ok';
  1732.     document.write(i.bold()+'<br/>');
  1733.     document.write(j.bold()+'<br/>');
  1734.     document.write(k.bold()+'<br/>');
  1735.     document.write(l.bold()+'<br/>');
  1736. //]]>
  1737. </script>
  1738.  
  1739. -2.2 全局变量与局部变量
  1740. <script type="text/javascript">
  1741. //<![CDATA[
  1742.     var i='global i';//全局
  1743.     function t(){
  1744.         var i='local i'; //局部
  1745.         document.write(i+'<br/>');
  1746.         j='global j' //全局
  1747.         document.write(j+'<br/>');
  1748.     }
  1749.     document.write(i+'<br/>');
  1750.     t();
  1751.     document.write(i+'<br/>');
  1752.     document.write(j+'<br/>');
  1753.     
  1754.     var k='global k'
  1755.     document.write(k+'<br/>');
  1756.     function t1(){
  1757.         k='change global k'
  1758.         document.write(k+'<br/>');
  1759.     }
  1760.     t1();
  1761.     document.write(k+'<br/>');
  1762. //]]>
  1763. </script>
  1764.  
  1765. -2.3 在函数中使用 var 定义一个变量, 该变量将影响整个函数, 不分先后. 
  1766. var scope = "global";
  1767. function f(  ) {
  1768.     alert(scope);         // Displays "undefined", not "global"
  1769.     var scope = "local";  // Variable initialized here, but defined everywhere
  1770.     alert(scope);         // Displays "local"
  1771. }
  1772. f(  ); 
  1773.  
  1774. -2.4 同时初始化多个变量并赋相同值
  1775. <script type="text/javascript">
  1776. //<![CDATA[
  1777.     var i=j=k=l=0;
  1778.     document.write(i+' '+j+' '+k+' '+l+' ');
  1779. //]]>
  1780. </script>
  1781.  
  1782. -3. 优先级
  1783.  
  1784. -3.1 函数比其他代码优先执行
  1785. <script type="text/javascript">
  1786. //<![CDATA[
  1787.     document.write((t(10)+'').bold()+'<br/>'); 
  1788.     t=10;
  1789.     document.write((t+'').bold()+'<br/>'); 
  1790.     function t(n){
  1791.         return n*n;
  1792.     }
  1793. //]]>
  1794. </script>
  1795.  
  1796. ---/---------------------------------------------------------
  1797.  
  1798. 常用参数
  1799.  
  1800. 3. document
  1801.  
  1802. 2. location
  1803.  
  1804. 1. this
  1805.  
  1806. ---------------------------------
  1807.  
  1808. 3.3 写出字符串
  1809. document.write("<input type='button' value='url' οnclick='addcontent('','');'/>");
  1810.  
  1811. 3.2 document.from
  1812. 指定 表单域
  1813.  
  1814. 3.1 document.all.id
  1815. 查找指定 HTML 页面 ID 或 名字
  1816.  
  1817. 3. document
  1818.  
  1819. 2.2 转到特定网页
  1820. onClick="location.href=' http://127.0.0.1/'"
  1821.  
  1822. 2.1 单独 location
  1823. 取页面地址
  1824.  
  1825. 2. location
  1826.  
  1827. 1.4 this.height
  1828. 取高度属性
  1829.  
  1830. 1.3 this.width
  1831. 取宽度属性
  1832.  
  1833. 1.2 this.href
  1834. 取链接地址
  1835.  
  1836. 1.1 this.src
  1837. 取图片地址
  1838.  
  1839. 1. this
  1840.  
  1841. ---/---------------------------------------------------------
  1842.  
  1843. 问题集:
  1844.  
  1845. 3. return 返回多个值 2006-10-14 14:13:18
  1846.  
  1847. 2. 理解 setInternal 用法
  1848.  
  1849. 1. 理解 void 用法  
  1850.  
  1851. ---/---------------------------------------------------------
  1852.  
  1853. 相关摘要:
  1854.  
  1855. 24. Keep in mind that the function statement is available in all versions of JavaScript, the Function( ) constructor is available only in JavaScript 1.1 and later, and function literals are available only in JavaScript 1.2 and later. Recall that we said the three functions defined earlier are "more or less" equivalent -- there are some differences between these three techniques for function definition, which we'll consider in Section 11.5.
  1856.  
  1857. //JavaScript: The Definitive Guide, 4th Edition -- 7.1.3 Function Literals
  1858. 2006-10-19 14:35:56
  1859.  
  1860. 23. The Function( ) constructor expects any number of string arguments. The last argument is the body of the function -- it can contain arbitrary JavaScript statements, separated from each other by semicolons. All other arguments to the constructor are strings that specify the names of the parameters to the function being defined. If you are defining a function that takes no arguments, you simply pass a single string -- the function body -- to the constructor.
  1861.  
  1862. //JavaScript: The Definitive Guide, 4th Edition -- 7.1.2 The Function( ) Constructor
  1863. 2006-10-19 14:16:18
  1864.  
  1865. 22. Note that ECMAScript v3 does not allow function definitions to appear anywhere; they are still restricted to top-level global code and top-level function code. This means that function definitions may not appear within loops or conditionals, for example.[1] These restrictions on function definitions apply only to function declarations with the function statement. As we'll discuss later in this chapter, function literals (another feature introduced in JavaScript 1.2 and standardized by ECMAScript v3) may appear within any JavaScript expression, which means that they can appear within if and other statements.
  1866.  
  1867. //JavaScript: The Definitive Guide, 4th Edition -- 7.1 Defining and Invoking Functions
  1868. 2006-10-19 14:10:13
  1869.  
  1870. 21. try and finally can be used together without a catch clause. In this case, the finally block is simply cleanup code that is guaranteed to be executed, regardless of any break, continue, or return statements within the try clause. For example, the following code uses a try/finally statement to ensure that a loop counter variable is incremented at the end of each iteration, even when an iteration terminates abruptly because of a continue statement:
  1871.  
  1872. //JavaScript: The Definitive Guide, 4th Edition -- 6.17 try/catch/finally
  1873. 2006-10-18 19:18:08
  1874.  
  1875. 20. The continue statement, in both its labeled and unlabeled forms, can be used only within the body of a while, do/while, for, or for/in loop. Using it anywhere else causes a syntax error.
  1876.  
  1877. //JavaScript: The Definitive Guide, 4th Edition -- 6.12 continue
  1878. 2006-10-18 16:03:46
  1879.  
  1880. 19. The following rules are used to determine whether two values are identical according to the === operator: 
  1881.  
  1882. If the two values have different types, they are not identical. 
  1883.  
  1884. If both values are numbers and have the same value, they are identical, unless either or both values are NaN, in which case they are not identical. The NaN value is never identical to any other value, including itself! To check whether a value is NaN, use the global isNaN( ) function. 
  1885.  
  1886. If both values are strings and contain exactly the same characters in the same positions, they are identical. If the strings differ in length or content, they are not identical. Note that in some cases, the Unicode standard allows more than one way to encode the same string. For efficiency, however, JavaScript string comparison compares strictly on a character-by-character basis, and it assumes that all strings have been converted to a "normalized form" before they are compared. See the "String.localeCompare( )" reference page in the core reference section of this book for another way to compare strings. 
  1887.  
  1888. If both values are the boolean value true or both are the boolean value false, they are identical. 
  1889.  
  1890. If both values refer to the same object, array, or function, they are identical. If they refer to different objects (or arrays or functions) they are not identical, even if both objects have identical properties or both arrays have identical elements. 
  1891.  
  1892. If both values are null or both values are undefined, they are identical. 
  1893.  
  1894. The following rules are used to determine whether two values are equal according to the == operator: 
  1895.  
  1896. If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal. 
  1897.  
  1898. If the two values do not have the same type, they may still be equal. Use the following rules and type conversions to check for equality: 
  1899.  
  1900. If one value is null and the other is undefined, they are equal. 
  1901.  
  1902. If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value. 
  1903.  
  1904. If either value is true, convert it to 1 and try the comparison again. If either value is false, convert it to 0 and try the comparison again. 
  1905.  
  1906. If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its toString( ) method or its valueOf( ) method. The built-in classes of core JavaScript attempt valueOf( ) conversion before toString( ) conversion, except for the Date class, which performs toString( ) conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way. 
  1907.  
  1908. Any other combinations of values are not equal. 
  1909.  
  1910. As an example of testing for equality, consider the comparison: 
  1911. "1" == true 
  1912.  
  1913. This expression evaluates to true, indicating that these very different-looking values are in fact equal. The boolean value true is first converted to the number 1, and the comparison is done again. Next, the string "1" is converted to the number 1. Since both numbers are now the same, the comparison returns true. 
  1914.  
  1915. When the equality operator in JavaScript 1.1 attempted to convert a string to a number and failed, it displayed an error message noting that the string could not be converted, instead of converting the string to NaN and returning false as the result of the comparison. This bug has been fixed in JavaScript 1.2.
  1916.  
  1917. //JavaScript: The Definitive Guide, 4th Edition -- 5.4 Equality Operators
  1918. 2006-10-16 21:56:30
  1919.  
  1920. 18. In top-level code (i.e., JavaScript code that is not part of a function), you can use the JavaScript keyword this to refer to the global object. Within functions, this has a different use, which is described in Chapter 7.
  1921.  
  1922. //JavaScript: The Definitive Guide, 4th Edition -- 4.6 Variables as Properties
  1923. 2006-10-16 16:08:34
  1924.  
  1925. 17. Garbage collection is automatic and is invisible to the programmer. You can create all the garbage objects you want, and the system will clean up after you! You need to know only enough about garbage collection to trust that it works; you don't have to wonder about where all the old objects go. For those who aren't satisfied, however, Section 11.3, contains further details on the JavaScript garbage-collection process.
  1926.  
  1927. //JavaScript: The Definitive Guide, 4th Edition -- 4.5 Garbage Collection
  1928. 2006-10-16 16:01:55
  1929.  
  1930. 16. Primitive Types and Reference Types
  1931. var a = [1,2,3];  // Initialize a variable to refer to an array
  1932. var b = a;        // Copy that reference into a new variable
  1933. a[0] = 99;        // Modify the array using the original reference
  1934. alert(b);         // Display the changed array [99,2,3] using the new reference
  1935.  
  1936. If this result does not seem surprising to you, you're already well familiar with the distinction between primitive and reference types. If it does seem surprising, take a closer look at the second line. Note that it is the reference to the array value, not the array itself, that is being assigned in this statement. After that second line of code, we still have only one array object; we just happen to have two references to it.
  1937.  
  1938. //JavaScript: The Definitive Guide, 4th Edition -- 4.4 Primitive Types and Reference Types
  1939.  
  1940. 15. 在任何情况下,如果六个月以后您还能毫不费力地阅读和理解所编写的代码,则说明这些代码写得不错。
  1941.  
  1942. //Windows 脚本技术 -- JScript -- 什么是 JScript?
  1943. 2006-10-16 14:12:55
  1944.  
  1945. 14. The rule that all variables declared in a function are defined throughout the function can cause surprising results. The following code illustrates this: 
  1946.  
  1947. var scope = "global";
  1948. function f(  ) {
  1949.     alert(scope);         // Displays "undefined", not "global"
  1950.     var scope = "local";  // Variable initialized here, but defined everywhere
  1951.     alert(scope);         // Displays "local"
  1952. }
  1953. f(  );  
  1954. //JavaScript: The Definitive Guide, 4th Edition -- 4.3 Variable Scope
  1955.  
  1956. 13. Variable Scope
  1957. The scope of a variable is the region of your program in which it is defined. A global variable has global scope -- it is defined everywhere in your JavaScript code. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function. 
  1958. Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable. For example, the following code prints the word "local": 
  1959.  
  1960. var scope = "global";         // Declare a global variable
  1961. function checkscope(  ) {
  1962.     var scope = "local";      // Declare a local variable with the same name
  1963.     document.write(scope);    // Use the local variable, not the global one
  1964. }
  1965. checkscope(  );               // Prints "local" 
  1966.  
  1967. //JavaScript: The Definitive Guide, 4th Edition -- 4.3 Variable Scope
  1968. 2006-10-16 1:11:59
  1969.  
  1970. 12. In general, functions do not know what variables are defined in the global scope or what they are being used for. Thus, if a function uses a global variable instead of a local one, it runs the risk of changing a value upon which some other part of the program relies. Fortunately, avoiding this problem is simple: declare all variables with var
  1971.  
  1972. //JavaScript: The Definitive Guide, 4th Edition -- 4.3 Variable Scope
  1973. 2006-10-15 22:23:32
  1974.  
  1975. 11. If you attempt to read the value of an undeclared variable, JavaScript will generate an error. If you assign a value to a variable that you have not declared with var, JavaScript will implicitly declare that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function. To prevent the creation of a global variable (or the use of an existing global variable) when you meant to create a local variable for use within a single function, you must always use the var statement within function bodies. It's best to use var for all variables, whether global or local. (The distinction between local and global variables is explored in more detail in the next section.) 
  1976.      
  1977. //JavaScript: The Definitive Guide, 4th Edition -- 4.2.1 Repeated and Omitted Declarations
  1978. 2006-10-15 22:00:53
  1979.  
  1980. 10. Error Objects
  1981.  
  1982. ECMAScript v3 defines a number of classes that represent errors. The JavaScript interpreter "throws" an object of one of these types when a runtime error occurs. (See the throw and try statements in Chapter 6 for a discussion of throwing and catching errors.) Each error object has a message property that contains an implementation-specific error message. The types of predefined error objects are Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, and URIError. You can find out more about these classes in the core reference section of this book.
  1983.  
  1984. //JavaScript: The Definitive Guide, 4th Edition -- 3.11 Error Objects
  1985. 2006-10-15 19:39:08
  1986.  
  1987. 9. undefined
  1988.  
  1989. Another special value used occasionally by JavaScript is the undefined value returned when you use either a variable that has been declared but never had a value assigned to it, or an object property that does not exist. Note that this special undefined value is not the same as null. 
  1990.  
  1991. Although null and the undefined value are distinct, the == equality operator considers them to be equal to one another. Consider the following: 
  1992. my.prop == null 
  1993.  
  1994. //JavaScript: The Definitive Guide, 4th Edition -- 3.8 undefined
  1995. 2006-10-15 18:02:39
  1996.  
  1997. 8. The way to really learn a new programming language is to write programs with it. 
  1998.  
  1999. //JavaScript: The Definitive Guide, 4th Edition -- 1.10 Exploring JavaScript
  2000. 2006-10-14 2:40:02
  2001.  
  2002. 7. Both Netscape and Microsoft have made their JavaScript interpreters available to companies and programmers who want to embed them in their applications. Netscape's interpreter was released as open source and is now available through the Mozilla organization (see   http://www.mozilla.org/js/). Mozilla actually provides two different versions of the JavaScript 1.5 interpreter. One is written in C and is called "SpiderMonkey." The other is written in Java and, in a flattering reference to this book, is called "Rhino."
  2003.  
  2004. //JavaScript: The Definitive Guide, 4th Edition -- 1.4 JavaScript in Other Contexts
  2005. 2006-10-14 1:13:44
  2006.  
  2007. 6. 11.1.2 Explicit Type Conversions
  2008.  
  2009. Table 11-1 listed the automatic data type conversions that JavaScript performs. It is also possible to explicitly convert values from one type to another. JavaScript does not define a cast operator as C, C++, and Java do, but it does provide similar facilities for converting data values. 
  2010.  
  2011. As of JavaScript 1.1 (and the ECMA-262 standard), Number( ) , Boolean( ), String( ), and Object( ) may be called as functions as well as being invoked as constructors. When invoked in this way, these functions attempt to convert their arguments to the appropriate type. For example, you could convert any value x to a string with String(x) and convert any value y to an object with Object(y). 
  2012.  
  2013. There are a few other tricks that can be useful for performing explicit conversions. To convert a value to a string, concatenate it with the empty string: 
  2014. var x_as_string = x + ""; 
  2015.  
  2016. To force a value to a number, subtract zero from it: 
  2017. var x_as_number = x - 0; 
  2018.  
  2019. And to force a value to boolean, use the ! operator twice: 
  2020. var x_as_boolean = !!x;
  2021.  
  2022. Because of JavaScript's tendency to automatically convert data to whatever type is required, explicit conversions are usually unnecessary. They are occasionally helpful, however, and can also be used to make your code clearer and more precise.
  2023.  
  2024. //JavaScript: The Definitive Guide, 4th Edition -- 11.1 Data Type Conversion
  2025. 2006-10-12 12:36:23
  2026.  
  2027. 5. Each RegExp object has five properties. The source property is a read-only string that contains the text of the regular expression. The global property is a read-only boolean value that specifies whether the regular expression has the g flag. The ignoreCase property is a read-only boolean value that specifies whether the regular expression has the i flag. The multiline property is a read-only boolean value that specifies whether the regular expression has the m flag. The final property is lastIndex, a read-write integer. For patterns with the g flag, this property stores the position in the string at which the next search is to begin. It is used by the exec( ) and test( ) methods, as described in the previous section.
  2028.  
  2029. //JavaScript: The Definitive Guide, 4th Edition -- 10.3.2 RegExp Instance Properties
  2030. 2006-10-9 21:23:26
  2031.  
  2032. 4. the RegExp constructor. search( ) does not support global searches -- it ignores the g flag of its regular expression argument.
  2033.  
  2034. //JavaScript: The Definitive Guide, 4th Edition -- 10.2 String Methods for Pattern Matching
  2035. 2006-10-9 19:56:40
  2036.  
  2037. 3. We've seen the . operator used to access the properties of an object. It is also possible to use the [] operator, which is more commonly used with arrays, to access these properties. Thus, the following two JavaScript expressions have the same value: 
  2038.  
  2039. object.property
  2040. object["property"] 
  2041.  
  2042. //JavaScript: The Definitive Guide, 4th Edition -- 8.6 Objects as Associative Arrays 
  2043. 2006-10-9 14:45:13
  2044.  
  2045. 2. The typeof Operator
  2046.  
  2047. typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. 
  2048.  
  2049. The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value. It evaluates to "object" for objects, arrays, and (surprisingly) null. It evaluates to "function" for function operands and to "undefined" if the operand is undefined.
  2050. //JavaScript: The Definitive Guide, 4th Edition -- 5.10.2 The typeof Operator 
  2051.  
  2052. 1. JavaScript strings (and JavaScript arrays, as we'll see later) are indexed starting with zero.
  2053.  
  2054.  
  2055. ---/---------------------------------------------------------
  2056.  
  2057. 小小心得
  2058.  
  2059. 1. 花括弧定义 对象 元素, 方括弧定义 数组 元素. 2006-10-15 17:55:50

2. Javascript 正则表达式摘要
  1. linenum
  2. Javascript 与正则表达式 By shawl.qiu 
  3.  
  4.  
  5. 语法方法:
  6.  
  7. 5. str.split()
  8. <script type="text/javascript">
  9. //<![CDATA[
  10.     var str=' this  , is, a, text';
  11.     document.write(str.split(//s*,/s*/));
  12.     document.write('<br/>');
  13.     document.write(str.split(//s*,/s*/).join());
  14. //]]>
  15. </script>
  16.  
  17. 4. str.match()
  18. <script type="text/javascript">
  19. //<![CDATA[
  20.     var str=' This is a test. ';
  21.         document.write(str.match(//w+/).join());
  22.         document.write('<br/>');
  23.         document.write(str.match(//w+/g).join());
  24. //]]>
  25. </script>
  26.  
  27. 3. str.replace()
  28. <script type="text/javascript">
  29. //<![CDATA[
  30.     var str=' This is a test. ';
  31.         document.write(str.replace(/^(/s+)|(/s+)$/,'----------'));
  32.         document.write('<br/>');
  33.         document.write(str.replace(/^(/s+)|(/s+)$/g,'----------'));
  34. //]]>
  35. </script>
  36.  
  37. 2. str.search()
  38. <script type="text/javascript">
  39. //<![CDATA[
  40.     var str=' This is a test. ';
  41.         document.write(str.search(/this/i));
  42. //]]>
  43. </script>
  44.  
  45. 1. 子匹配
  46. <script type="text/javascript">
  47. //<![CDATA[
  48.     var str=' this is a test '
  49.     document.write(str.match(/(is+).*?/1/)[0]);
  50. //]]>
  51. </script>
  52.  
  53. 0. 创建正则表达式模式
  54.  
  55. 0.1 new RegExp()
  56. <script type="text/javascript">
  57. //<![CDATA[
  58.     var str=' this is a test '
  59.     var re=new  RegExp('//w+','g');
  60.     document.write(str.match(re));
  61. //]]>
  62. </script>
  63.  
  64. -1. re.exec(str)
  65. <script type="text/javascript">
  66. //<![CDATA[
  67.     var str=' this is a test '
  68.     var pt=/this|test/g;
  69.     
  70.     var report
  71.     while((report=pt.exec(str))!=null){
  72.         document.write(('match: ').bold()+report[0].fontcolor('red')+'<br/>');
  73.         document.write(('match index: ').bold()+(report.index+'').fontcolor('red')+'<br/>');
  74.         document.write(('match lastIndex: ').bold()+(pt.lastIndex+'').fontcolor('red')+'<p/>');
  75.     }
  76. //]]>
  77. </script>
  78.  
  79. -2. re.test(str)
  80. <script type="text/javascript">
  81. //<![CDATA[
  82.     var str=' this is a test ';
  83.     var re=//d+/;
  84.     document.write(re.test(str));
  85.     
  86.     var re=//w+/;
  87.     document.write(re.test(str));
  88. //]]>
  89. </script>
  90.   
  91. ---/----------------------------------------
  92.  
  93. 摘要:
  94.  
  95. 4. The String methods search( ) , replace( ), and match( ) do not use the lastIndex property as exec( ) and test( ) do. In fact, the String methods simply reset lastIndex( ) to 0. If you use exec( ) or test( ) on a pattern that has the g flag set and you are searching multiple strings, you must either find all the matches in each string, so that lastIndex is automatically reset to zero (this happens when the last search fails), or you must explicitly set the lastIndex property to 0 yourself. If you forget to do this, you may start searching a new string at some arbitrary position within the string rather than from the beginning. Finally, remember that this special lastIndex behavior occurs only for regular expressions with the g flag. exec( ) and test( ) ignore the lastIndex property of RegExp objects that do not have the g flag.
  96.  
  97. //JavaScript: The Definitive Guide, 4th Edition -- 10.3.1 RegExp Methods for Pattern Matching
  98. 2006-10-20 18:10:36
  99.  
  100. 3. match method
  101. The match( ) method is the most general of the String regular expression methods. It takes a regular expression as its only argument (or converts its argument to a regular expression by passing it to the RegExp( ) constructor) and returns an array that contains the results of the match. If the regular expression has the g flag set, the method returns an array of all matches that appear in the string. For example: 
  102. "1 plus 2 equals 3".match(//d+/g)  // returns ["1", "2", "3"] 
  103.  
  104. //JavaScript: The Definitive Guide, 4th Edition -- 10.2 String Methods for Pattern Matching
  105. 2006-10-20 17:23:08
  106.  
  107. 2. search method
  108. Strings support four methods that make use of regular expressions. The simplest is search( ). This method takes a regular expression argument and returns either the character position of the start of the first matching substring, or -1 if there is no match. For example, the following call returns 4: 
  109. "JavaScript".search(/script/i); 
  110.  
  111. If the argument to search( ) is not a regular expression, it is first converted to one by passing it to the RegExp constructor. search( ) does not support global searches -- it ignores the g flag of its regular expression argument.
  112.  
  113. //JavaScript: The Definitive Guide, 4th Edition -- 10.2 String Methods for Pattern Matching
  114.  
  115. 1. A number of punctuation characters have special meanings in regular expressions. They are: 
  116. ^ $ . * + ? = ! : | / / ( ) [ ] { }  
  117. //JavaScript: The Definitive Guide, 4th Edition -- 10.1 Defining Regular Expressions
  118. 2006-10-9 19:39:54


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值