序列化:
把JS對象轉(zhuǎn)換成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
轉(zhuǎn)義:
把URL的中文轉(zhuǎn)義成編碼绿聘。一般用于轉(zhuǎn)義數(shù)據(jù)后塞祈,保存在本地Cookies
encodeURI( ) :轉(zhuǎn)義URL字符,轉(zhuǎn)中文字和常見符號辜王,不轉(zhuǎn)!@#$&*()_+-=.
encodeURIComponent( ):轉(zhuǎn)義URL字符,除了~!*()-_.罐孝,其它都轉(zhuǎn)
decodeURI( ):還原decodeURI中的轉(zhuǎn)義字符
decodeURIComponent( ):還原decodeURIcomponent組件中的字符
escape( ):對字符串轉(zhuǎn)義
unescape( ):給轉(zhuǎn)義字符串解碼
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和exec:
eval():執(zhí)行字符表達(dá)式或代碼塊莲兢,
執(zhí)行表達(dá)式返回結(jié)果汹来;
執(zhí)行代碼塊返回最后一個帶=號變量的結(jié)果续膳,代碼塊的變量運(yùn)算可以改變外面代碼的變量值
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
作用域:
以函數(shù)做為作用域,函數(shù)內(nèi)變量在函數(shù)沒調(diào)用前就已經(jīng)聲明(變量沒有值收班,值是undefined)
作用域鏈:變量值尋找方式坟岔,先找自已-再找父-再找爺爺。
//作用域鏈摔桦,子函數(shù)存在a變量
var a='grand';function func(){
? var a='parent';
? function sub(){
? ? var a='son';
? ? document.write(a)
? }
? return sub;}
? var b=func()
? b()? ? ? ? ? ? ? ? //son
//作用域鏈例子社付,變量在子函數(shù)運(yùn)行前調(diào)用
var a='grand';
function func(){
? var a='parent';
? function sub(){
? ? document.write(a)
? }
? var a='son';
? return sub;? ? //在返回的函數(shù)前
? }
? var b=func()
? b()? ? ? ? ? ? //son
//函數(shù)內(nèi)的局部變量,調(diào)用時先聲明變量邻耕,聲明
var bfunction func(){
? document.write(b);
? var b='s'? ? ? ? ? ? // b在調(diào)用后聲明瘦穆,但程序并不報錯
}
func();? ? ? ? // undefined
對象的this:類似于python類的self
使用new創(chuàng)建一個對象,就可以像python類一樣調(diào)用屬性,并且可以創(chuàng)建不同的實例
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();? ? ? ? ? ? ? ? ? ? //把子函數(shù)當(dāng)做類方法調(diào)用
? f1.sayname();
prototype原型赊豌,可以給對象添加屬性
function Foo(n){
? this.name=n;
}
var f = new Foo('david');
Foo.prototype.age=19;
document.write(f.age)? ? ? ? //19
函數(shù):
1、基本函數(shù)
function func(){
? var a=1;
? document.write(a);
}
2绵咱、匿名函數(shù):沒有函數(shù)名碘饼。通常寫在其它函數(shù)或方法里
//例一
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、自執(zhí)行函數(shù)
無需調(diào)用悲伶,直接運(yùn)行
(function test(n){
? for (var i=1;i<10;i++){
? ? document.write(i,':',n+i,'<br>');
? }})(3)
4艾恼、閉包函數(shù):占用內(nèi)存大,盡量少用
普通函數(shù)的問題:函數(shù)做用域是在函數(shù)本身麸锉,當(dāng)?shù)诙握{(diào)用時钠绍,不能記住上次運(yùn)算的值 ,函數(shù)里的變量重新獲取值花沉。
閉包函數(shù)原理:自執(zhí)行func()后柳爽,func()對象保存在add變量里,add變量不消失碱屁,內(nèi)存就不回收func()磷脯, count的作用域是在func()內(nèi),count+=1也改變了var count的值 娩脾,所以實現(xiàn)累加赵誓。
var add=(function func(){
? var count=0;
? return function(){count+=1; return count;}})();
? document.write(add())
? document.write(add())
? document.write(add())
函數(shù)實現(xiàn)解讀:
1、var add 柿赊,聲明變量add俩功,作用域-全局,
2碰声、(func)()是自執(zhí)行函數(shù)诡蜓,執(zhí)行func,所以add=function(){count+=1;return count;}
3奥邮、運(yùn)行add()万牺,也就是運(yùn)行function()這個匿名函數(shù)罗珍,并改變count的值,count=2
4、重點(diǎn)來了脚粟,add變量保存的內(nèi)存對象繼續(xù)生效覆旱,也就是count作用域也繼續(xù)存在,所以核无,再次運(yùn)行add()扣唱,也就是再次運(yùn)行了function()這個匿名函數(shù),count再加1团南,count=3
更多web前端開發(fā)知識噪沙,請查閱 HTML中文網(wǎng) !吐根!