JAVAScript学习2-序列化、转义和函数

发布时间:2020-07-05 09:14:17 作者:等你的破船
来源:网络 阅读:294

序列化:

    把JS对象转换成JSON格式

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

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' onclick='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


推荐阅读:
  1. mysql学习笔记(2-初始化)
  2. python内置函数和序列化

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

闭包;js函数 js闭包 js作用域

上一篇:php中生成唯一标示符的三种方法

下一篇:Go语言之常量与运算符

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》