序列化:
把 JS 对象转换成 JSON 格式
JSON.stringify(obj) 序列化
JSON.parse(str) 反序列化
- var ls=[1,2,3,4];
- var ls_st = JSON.stringify(ls);
- var ls_ls = JSON.parse(ls_st);
- document.write(ls_st); //[1,2,3,4]
- document.write(ls_ls); //1,2,3,4
转义:
把 URL 的中文转义成编码. 一般用于转义数据后, 保存在本地 Cookies
encodeURI( ) : 转义 URL 字符, 转中文字和常见符号, 不转!@#$&*()_+-=.
encodeURIComponent( ): 转义 URL 字符, 除了~!*()-_., 其它都转
decodeURI( ): 还原 decodeURI 中的转义字符
decodeURIComponent( ): 还原 decodeURIcomponent 组件中的字符
escape( ): 对字符串转义
unescape( ): 给转义字符串解码
URIError: 由 URl 的编码和解码方法抛出
- var web_address='http://localhost:8080/test.html?user = 管理员';
- var web_encode=encodeURI(web_address);
- document.write(web_encode);//http://localhost:8080/test.HTML?user=管理员
- var web_encode=decodeURI(web_encode);
- document.write('<br>');
- document.write(web_encode); //http://localhost:8080/test.HTML?user = 管理员
- document.write('<br>');
- var web_en_c=encodeURIComponent(web_address); // 原始地址 encode
- //http://localhost:8080/test.HTML?user=管理员
- document.write('<br>');
- var web_ene_c=encodeURIComponent(web_encode); //encodeURI,encode, 把 % 号也 encode 了
- //http://localhost:8080/test.HTML?user=%E7%AE%A1%E7%90%86%E5%91%98
- document.write('<br>');
- var web_de_c=decodeURIComponent(web_en_c);
- //http://localhost:8080/test.HTML?user = 管理员
- document.write('<br>');
- var web_dee_c=decodeURIComponent(web_ene_c); //decode 一层, 汉字需要再次 decode/decodeURIC 也可以
- //http://localhost:8080/test.HTML?user=管理员
eval 和 exec:
eval(): 执行字符表达式或代码块,
执行表达式返回结果;
执行代码块返回最后一个带 = 号变量的结果, 代码块的变量运算可以改变外面代码的变量值
- var a;
- var multiple='var a=1;a=a+1;var b=1;b=b+6;'
- var result=eval(multiple);
- document.write(result); // 7
- document.write(a); // 2
作用域:
以函数做为作用域, 函数内变量在函数没调用前就已经声明 (变量没有值, 值是 undefined)
作用域链: 变量值寻找方式, 先找自已 - 再找父 - 再找爷爷.
- // 作用域链, 子函数存在 a 变量
- var a='grand';function func(){
- var a='parent';
- function sub(){
- var a='son';
- document.write(a)
- }
- return sub;}
- var b=func()
- b() //son
- // 作用域链例子, 变量在子函数运行前调用
- var a='grand';
- function func(){
- var a='parent';
- function sub(){
- document.write(a)
- }
- var a='son';
- return sub; // 在返回的函数前
- }
- var b=func()
- b() //son
- // 函数内的局部变量, 调用时先声明变量, 声明
- var bfunction func(){
- document.write(b);
- var b='s' // b 在调用后声明, 但程序并不报错
- }
- func(); // undefined
对象的 this: 类似于 python 类的 self
使用 new 创建一个对象, 就可以像 python 类一样调用属性, 并且可以创建不同的实例
- function Foo(n){
- this.name=n;
- this.sayname=function (){
- document.write(this.name)
- }
- }
- var f = new Foo('david');
- document.write(f.name); //daivd
- var f1 = new Foo('sam');
- document.write(f1.name); // sam
- f.sayname(); // 把子函数当做类方法调用
- f1.sayname();
prototype 原型, 可以给对象添加属性
- function Foo(n){
- this.name=n;
- }
- var f = new Foo('david');
- Foo.prototype.age=19;
- document.write(f.age) //19
函数:
1, 基本函数
- function func(){
- var a=1;
- document.write(a);
- }
2, 匿名函数: 没有函数名. 通常写在其它函数或方法里
- // 例一
- function Foo(n){
- this.name=n;
- this.action=function(){
- document.write('papapa');
- }
- }
- var f = new Foo('david');
- f.action()// 例二
- var i=1;
- setInterval(function(){i++;alert(i);},2000);
3, 自执行函数
无需调用, 直接运行
- (function test(n){
- for (var i=1;i<10;i++){
- document.write(i,':',n+i,'<br>');
- }})(3)
4, 闭包函数: 占用内存大, 尽量少用
普通函数的问题: 函数做用域是在函数本身, 当第二次调用时, 不能记住上次运算的值 , 函数里的变量重新获取值.
闭包函数原理: 自执行 func() 后, func() 对象保存在 add 变量里, add 变量不消失, 内存就不回收 func(), count 的作用域是在 func() 内, count+=1 也改变了 var count 的值 , 所以实现累加.
- var add=(function func(){
- var count=0;
- return function(){count+=1; return count;}})();
- document.write(add())
- document.write(add())
- document.write(add())
函数实现解读:
1,var add , 声明变量 add, 作用域 - 全局,
2,(func)() 是自执行函数, 执行 func, 所以 add=function(){count+=1;return count;}
3, 运行 add(), 也就是运行 function() 这个匿名函数, 并改变 count 的值, count=2
4, 重点来了, add 变量保存的内存对象继续生效, 也就是 count 作用域也继续存在, 所以, 再次运行 add(), 也就是再次运行了 function() 这个匿名函数, count 再加 1,count=3
来源: http://www.css88.com/web/javascript/11020.html