序列化:

    把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=%E7%AE%A1%E7%90%86%E5%91%98
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%3A%2F%2Flocalhost%3A8080%2Ftest.html%3Fuser%3D%E7%AE%A1%E7%90%86%E5%91%98
document.write('<br>');
var web_ene_c=encodeURIComponent(web_encode);       //encodeURI,encode,把%号也encode了
        //http%3A%2F%2Flocalhost%3A8080%2Ftest.html%3Fuser%3D%25E7%25AE%25A1%25E7%2590%2586%25E5%2591%2598      
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=%E7%AE%A1%E7%90%86%E5%91%98


eval:

    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

alert和confirm:

    alert:弹出警告提示框

    confirm:确定提示框,确定返回True,取消返回False

alert('已删除');

var t_notice=confirm('确定删除吗') //选择确定,返回true;选择取消,返回false.

location:

    location.href:获取当前网址;

    location.href='网址':跳转到指定网址;

    location.reload():页面刷新

var l=location.href;         //l是当前地址
location.href='http://www.baidu.com'        //页面跳转到百度


setInterval()和clearInterval();

    setInterval():设置定时器

    clearInterval():删除定时器

<div type='submit' οnclick='stoped()' value='停止'>
<script>
    var t_start=setInterval(function(){alert('提示~');},2000);
    function stoped(){
        clearInterval(t_start);        //点停止调用函数清除定时器
    }
</script>



作用域:

    以函数做为作用域,函数内变量在函数没调用前就已经声明(变量没有值,值是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 b
function 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