专注于前端开发、交互设计、用户体验

Object / Array to JSON

调试代码时,常常需要将数据读出,这个小工具或许能帮上忙.

  1. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>  
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  3.   
  4. <head>  
  5. <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8″ />  
  6. <title>Untitled 1</title>  
  7. <script type=“text/javascript”>  
  8. <!–  
  9.   
  10. function tojson(target) {  
  11.     var str = [],  
  12.         i,  
  13.         len;  
  14.     if (Object.prototype.toString.call(target)===‘[object Array]‘) {  
  15.         for (i=0,len=target.length; i<len; i++) {  
  16.             str.push(tojson(target[i]));  
  17.         }  
  18.         str = ‘['+str.join(',')+']‘;  
  19.     }  
  20.     else if (!!target  && (typeof target == ‘object’ || typeof target == ‘function’)) {  
  21.         for (i in target) {  
  22.             if (!i && !target[i]) {  
  23.                 continue;  
  24.             }  
  25.             else {  
  26.                 str.push(‘”‘+String(i).toString().replace(/\”/g,‘\\”‘)+‘”:’+tojson(target[i]));  
  27.             }  
  28.         }  
  29.         str = ‘{‘+str.join(‘,’)+‘}’;  
  30.     }  
  31.     else if ( target === undefined){  
  32.         str = ‘undefined’;  
  33.     }  
  34.     else if ( target === null){  
  35.         str = ‘null’;  
  36.     }  
  37.     else {  
  38.         str = ‘”‘+String(target).toString().replace(/\”/g,‘\\”‘)+‘”‘;  
  39.     }  
  40.     return str;  
  41. }  
  42.   
  43. function doit(){  
  44.     //todo  
  45.     alert(tojson({‘a’:[{'"':123,'c':undefined}]}));  
  46. }  
  47.   
  48. //–>  
  49. </script>  
  50. </head>  
  51.   
  52. <body><button type=“button” onclick=“doit()”>doit</button>  
  53.   
  54. </body>  
  55.   
  56. </html>  

Python内部函数无法修改在外部函数中定义的变量

转载自:http://blog.iamzsx.me/show.html?id=83004

在python中我们可以在一个函数中再定义一个函数,而内部的函数可以使用外部的局部变量。如下面的代码:

  1. def test():  
  2.     a=1  
  3.     def test1():  
  4.         print a  
  5.     a=2  
  6.     test1()  
  7.     a=3  
  8.     test1()  
  9.   
  10. test()  

输出结果是

2
3

这是一个很方便的特性,但是需要注意的是,在内部的函数中无法修改外部的局部变量。

例如下面的代码,就会报错:

  1. def test():  
  2.     a=1  
  3.     def test1():  
  4.         a+=2  
  5.         print a  
  6.     a=2  
  7.     test1()  
  8.     a=3  
  9.     test1()  
  10.   
  11. test()  

Unbound LocalError: local variable ‘a’ referenced before assignment

在python的FAQs中提到了这件事,如果一个变量在函数中被赋值,那么就被视为局部变量,也就是test1里面的a此时被认为是test1自己的局部变量,而我们没有给a赋值就使用了a,于是出现上面的异常信息。这也就是我前面说“无法修改”的原因,并不是不允许修改,而是无法使用到这个外部的变量(test的a是外部变量,test1中的a被当成了局部变量,在test1中我们看不见test中的a)。

在python 3.0之后,我们可以通过给一个变量加nonlocal标识符来告诉解释器,不要把这个变量当成函数内部的局部变量,从而可以修改外部变量。这也更证明了前面的无法修改的观点。3.0代码如下:

  1. def test():  
  2.     a=1  
  3.     def test1():  
  4.         nonlocal a  
  5.         a+=2  
  6.         print(a)  
  7.     a=2  
  8.     test1()  
  9.     a=3  
  10.     test1()  
  11.   
  12. test()  

关于python的闭包实现,请参考http://linluxiang.javaeye.com/blog/789946

返回顶部