?(一)函數(shù)
1.函數(shù)基本聲明
function fun([參數(shù)1],[參數(shù)2],...){
??? 函數(shù)體;
};
2.函數(shù)表達(dá)式洁桌,又叫函數(shù)字面量
var 變量=function([參數(shù)1],[參數(shù)2],...){
??? 函數(shù)體媚朦;
};
3.函數(shù)構(gòu)造法氧敢,需要new關(guān)鍵字詞
var 變量=new function("參數(shù)1","參數(shù)2",...,函數(shù)體);
(二)對象、屬性莲镣、數(shù)組等
1.
var o={
??? init:function(){
??????? this.bind();
??????? this.popup();
??? },
??? bind:function(){
??????? return "你好";
??? },
??? popup:function(){
??? }
};
//調(diào)用
o.init();
2.
var obj={};
obj.name="yaoyao";
obj['age']=23;
console.log(obj['name']);
console.log(obj['age']);
運(yùn)行結(jié)果:yaoyao
??????? ? ? ? ?? 23
ps:obj['name']等同于obj.name;兩者區(qū)別:其中.是取自身屬性福稳,[]的里面可以是變量
3.
var obj={};
obj.name="yaoyao";
var nameN='name';
console.log(obj.nameN);
console.log(obj.['nameN']);
console.log(obj.[nameN]);//obj.[nameN]==obj.'name'
運(yùn)行結(jié)果: undefined
????????? ? ? ? ? undefined
????????? ? ? ? ? yaoyao
4.刪除屬性;delete
//創(chuàng)建對象
var obj={};
obj.name="yaoyao";
//刪除屬性
delete obj.name;
console.log(obj['name']);
運(yùn)行結(jié)果:undefined
5.檢測屬性:in\hasOwnProperty
//創(chuàng)建對象
var obj={};
obj.name="yaoyao";
obj.age="undefined";
console.log(name in o);//檢測屬性是否屬于當(dāng)前對象瑞侮,運(yùn)算符in
console.log(obj.hasOwnProperty('name'));//檢測屬性,運(yùn)算符hasOwnProperty
console.log(obj.name!=undefined);//檢測屬性
console.log(obj.age!=undefined);
運(yùn)行結(jié)果: true
???????? ? ? ? ?? true
????????? ? ? ??? true
????????? ?? ? ?? false
6.對象遍歷的圆、枚舉屬性
var o={//對象
??? x:1,
??? y:2,
??? z:3
};
//數(shù)組對象
var arr=[{x:1},{y:2},{z:3}];
for(a in o){? //a是變量,for in遍歷出key
console.log(a);
}
for(a in o){? //a是變量半火,for in遍歷出value
console.log(o[a]);
}
for(a in arr){? //a是變量越妈,for in遍歷出數(shù)組的索引
console.log(a);
}
運(yùn)行結(jié)果: x
?????????? y
?????????? z
?????????? 1
?????????? 2
?????????? 3
?????????? 0
?????????? 1
?????????? 2
7.數(shù)組循環(huán)
var arr=new Array();
arr=[1.6,5,3,5,33];
for(var i=0;i
}
$.each(arr,function(index,item){//方法二,異步
})
8.序列化對象
ps:? 深拷貝和淺拷貝的區(qū)別:深拷貝--類似雙胞胎是兩個獨(dú)立的個體;淺拷貝--也就是引用钮糖,類似人和自己的影子對象數(shù)組都是淺拷貝梅掠。
ps:? 數(shù)據(jù)交互的數(shù)據(jù)類型是json\xml字符串等,不能是對象店归,所以要想傳送對象給服務(wù)器阎抒,需要序列化對象
var o={//對象
??? x:1,
??? y:2,
??? z:3
};
console.log(JSON.stringify(o));//對象轉(zhuǎn)換成字符串--stringify
console.log(typeof(JSON.stringify(o));//檢測數(shù)據(jù)類型
var str=JSON.stringify(o);//把字符串轉(zhuǎn)換成對象--parse
console.log(typeof(JSON.parse(str));//JSON.parse(str)是深拷貝,類似雙胞胎是兩個獨(dú)立的個體? (淺拷貝消痛,也就是引用且叁,類似人和自己的影子)
var 02={
??? x:1,
??? y:2,
??? z:3
}
var p=02;
o2.x='111';//淺拷貝--引用,隨之改變
console.log(p);
運(yùn)行結(jié)果:
??????? {"x":1,"y":2,"z":3}
??????? string
??????? object
??????? {x:"111",y:2,z:3}
9.jq的ajax實(shí)例
?$().ready(function () {
??????? $('#Login').click(function () {
??????????? if ($('#username').val() == "" || $('#password').val() == "") {
??????????????? alert("用戶名或密碼不能為空秩伞!");
??????????? }
??????????? else {
??????????????? $.ajax({
??????????????????? type: "POST",
??????????????????? url: "Ajax/LoginHandler.ashx",
??????????????????? data: "username=" + escape($('#username').val()) + "&password=" + escape($('#password').val()),
??????????????????? beforeSend: function () {
??????????????????????? $("#loading").css("display", "block"); //點(diǎn)擊登錄后顯示loading逞带,隱藏輸入框
??????????????????????? $("#login").css("display", "none");
??????????????????? },
??????????????????? success: function (msg) {
??????????????????????? $("#loading").hide(); //隱藏loading
??????????????????????? if (msg == "success") {
??????????????????????????? //parent.tb_remove();
??????????????????????????? parent.document.location.href = "admin.htm"; //如果登錄成功則跳到管理界面
??????????????????????????? parent.tb_remove();
??????????????????????? }
??????????????????????? if (msg == "fail") {
??????????????????????????? alert("登錄失敗纱新!");
??????????????????????? }
??????????????????? },
??????????????????? complete: function (data) {
??????????????????????? $("#loading").css("display", "none"); //點(diǎn)擊登錄后顯示loading展氓,隱藏輸入框
??????????????????????? $("#login").css("display", "block");
??????????????????? },
??????????????????? error: function (XMLHttpRequest, textStatus, thrownError) {
??????????????????? }
??????????????? });
??????????? }
??????? });
??? });
10.判斷arr數(shù)組對象里面是否有a---hasOwnProperty()
var arr=[{a:1,q:2,w:3},{d:1,r:2,c:3},{t:1,v:2,z:3}];
for(var i=0;i
??? if(arr[i].hasOwnProperty('a')){//hasOwnProperty檢測arr里main是否存在a
?????? console.log('yes');
?????? break;//跳出當(dāng)前循環(huán)
???? }else{
?????? console.log('no');
???? }
}
$.each(arr.function(index,item){//此時的item是對象
??? if(item.hasOwnProperty('a')){
?????? console.log('yes');???? ?
???? }else{
?????? console.log('no');
???? }
})
$.each(arr.function(index,item){//index是索引,此時的item是對象
??? if(item.a!=undefined){
?????? console.log('yes');???? ?
???? }else{
?????? console.log('no');
???? }
})
運(yùn)行結(jié)果: yes
??????? ? ? ? ? ? yes
??????? ? ? ? ? ? yes
(三)
1.原型--prototype(原型屬性)
ps: 每個函數(shù)都有一個原型屬性脸爱,在此原型屬性中都有一個構(gòu)造(constructor)函數(shù)遇汞,原型里面所共有的屬性對象實(shí)例化也有。
function Person(){
};
Person.prototype.name='yaoyao';
Person.prototype.age=22;
Person.prototype.action=function(){
??? console.log(this.name);//該this指當(dāng)前實(shí)例化的對象,并不是原型
};
var person1=new Person();
person1.name='ben';//局部改變
var person2=new Person();
console.log(person2.age);//輸出22
person1.__proto__.age=30;//通過__proto__改變age屬性值空入,全局發(fā)生了變化教寂,全局age就等于30
console.log(person1.name);//輸出ben
console.log(person2.name);//輸出yaoyao
console.log(person2.age);//輸出30
console.log(person2);//輸出:Person {}
???????????????????????????????? __proto__:
?????????????????????????????????? action:?()
?????????????????????????????????? age:30
?????????????????????????????????? name:"liangxue"
?????????????????????????????????? constructor:? Person()
?????????????????????????????????? __proto__:Object
console.log(Person.prototype.isPrototypeOf(person2));//輸出true--檢測當(dāng)前實(shí)例person2是否屬于該原型
2.構(gòu)造函數(shù)和原型混合使用
(1)單純使用構(gòu)造函數(shù)時(當(dāng)需要多個實(shí)例時就得new多個,會導(dǎo)致性能變差执庐,內(nèi)存溢出酪耕,所以需要使用原型)
//構(gòu)造函數(shù)
function Aa(name,age){
??? this.name=name;
??? this.age=age;
??? this.action=function(){
??????? return this.name;
??? }
};
var aa=new Aa('yaoyao',24);
var bb=new Aa('sushan',24);
console.log(aa.name);//輸出yaoyao
(2)結(jié)合原型使用
//構(gòu)造函數(shù)
function Aa(name,age,hobby){
??? this.name=name;
??? this.age=age;
??? this.hobby=hobby; ?
};
//原型--不會因?yàn)閷?shí)例化多個而變化
Aa.prototype.action=function(){
??? return '名字:'+this.name+',年齡:'+this.age+',愛好:'+this.hobby;
};
var aa=new Aa('yaoyao',24,'唱歌');
var bb=new Aa('sushan',24,'跳舞');
console.log(bb.action());//輸出--名字:sushan,年齡:24,愛好:跳舞
3.繼承(先繼承代溝最小的,依次往后)
(1)
function Anima(){}
Anima.prototype.canDo=function(){
??? console.log("吃飯睡覺");
}
function Cat(){}
Cat.prototype=new Anima();//Cat繼承Anima
var cat=new Cat();//cat不僅擁有Anima里面的也擁有Cat里面的轨淌。
cat.canDo();//輸出:吃飯睡覺
function Dog(){
??? this.canDo=function(){
??????? console.log("吃狗糧");
??? }
}
Dog.prototype=new Anima();//Dog繼承Anima
var dog=new Dog();
dog.canDo();//輸出:吃狗糧
cat1.canDo=function(){
console.log("貓吃魚");
};
cat1.canDo();//輸出:貓吃魚
(2)
function Fa(){}
Fa.prototype.name="fa";
Fa.prototype.age=23;
function Fb(){};
Fb.prototype=new Fa();
Fb.prototype.name="fb";
Fb.prototype.age=24;
function Fc(){};
Fc.prototype=new Fb();
Fc.prototype.name="fc";
var fc=new Fc();
console.log(fc.name);//輸出fc
fc.__proto__.name="fcc";//當(dāng)前有name迂烁,改變當(dāng)前
console.log(fc.name);//輸出fcc
console.log(fc.age);//輸出24,改變前
fc.__proto__.age=25;//當(dāng)前沒有age递鹉,改變有age且代溝最小的父類Fb
console.log(fc.age);//輸出25
var fa=new Fa();
console.log(fa.age);//輸出23盟步,未被改變
4.有參數(shù)時的構(gòu)造函數(shù)、原型躏结、繼承
function ParentType(name,age){
??? this.name=name;
??? this.age=age;
};
ParentType.prototype.getParentName=function(){
??? return this.name;
}
function SonType(){
ParentType.call(this,"yaoyao",23);//子類調(diào)用父類并傳參却盘,也可以使用apply(),作用都是在一個對象中調(diào)用另一個對象,區(qū)別在于所傳參數(shù)call(this,name,age)??? apply(this,數(shù)組)媳拴。
??? this.name='yy';
};
SonType.prototype=new ParentType();
SonType.prototype.getSonName=function(){
??? return this.name;
};
var aa=new SonType();
console.log(aa.name);//輸出yy黄橘,如果實(shí)例化里有name先取實(shí)例化的
console.log(aa.age);//輸出23
(四)作用域、閉包屈溉、遞歸塞关、this關(guān)鍵字
1.作用域---當(dāng)前所在區(qū)域,大可分為全局作用域子巾、局部作用域
var x="123";
function aa(){
??? var a='1';
}
function bb(){
??? var b='2';
??? console.log(b);//局部
??? console.log(x); //全局 ?
}
bb();//輸出2
?????????? 123
2.閉包--內(nèi)部和外部的函數(shù)聯(lián)系橋梁
(1)
?var x="s1";
?function aa(){
???? var x="s2";
???? function son(){
???????? var x="s3";
???????? return x;
???? };
???? return son();
?};
?console.log(aa());//輸出:s3
(2)
var x="s1";
?function aa(){
???? var x="s2";
???? return function son(){
???????? var x="s3";
???????? return x;
???? };? ?
?};
?console.log(aa());//輸出:? ? son(){
?????????????????????????????????? var x="s3";
?????????????????????????????????? return x;
????????????????????????????????? }
?console.log(aa()());//輸出:s3
(3)
?var fun=(function aa(){
???? var x="s2";
???? return function son(){
???????? var x="s3";
???????? return x;
???? };? ?
?}());
?console.log(fun());//輸出s3
3.遞歸
(1)
?function setup(x){
???? var i=0;
???? return function(){
???????? return x[i++];
???? }
?}
?var next=setup(['a','b','c']);
?console.log(next());//輸出a
?console.log(next());//輸出b
?console.log(next());//輸出c
(2)
var fun= (function setup(x){
??? var i=0;
??? return function(){
??????? return x[i++];
??? }
}(['a','b','c']));
console.log(fun());//輸出a
console.log(fun());//輸出b
console.log(fun());//輸出c
4.this關(guān)鍵字---指當(dāng)前調(diào)用函數(shù)的對象(函數(shù)的擁有者)
(1)函數(shù)中引用
var x=1;
function test(){
??? this.x;//指向window.x
};
test();
console.log(x);//輸出1
-----------------
var x=1;
function test(){
??? this.x=0;//指向window.x
};
test();
console.log(x);//輸出0
-----------------
var x=1;
function test(){
??? this.x=0;//指向window.x
};
function test1(){
??? this.x=1;//指向window.x
};
test();
test1();
console.log(x);//輸出1,根據(jù)執(zhí)行順序
(2)對象中調(diào)用
var o={};//創(chuàng)建對象
o.a=1;
o.action=function(){
??? return this.a;//this指向o對象
}
console.log(o.action());//輸出1
--------------------
對象中調(diào)用外部函數(shù)
function test(){
??? return this.a;//this指向o對象
}
var o={};//創(chuàng)建對象
o.a=1;
o.action=test;
console.log(o.action());//輸出1
--------------------
ps: 改變this指向帆赢,可以用call(),appy(),用于在當(dāng)前對象中調(diào)用其他對象线梗,兩者區(qū)別在于傳參不同椰于,appy(this,[])傳的是數(shù)組;call(this,age,name);
var a="111";
function test(){
??? return this.a;//this指向o對象
}
var o={};//創(chuàng)建對象
o.a=1;
o.b=test;
console.log(o.b.call());//輸出111仪搔,call()未傳參瘾婿,此時this指向window中的a
console.log(o.b.call(o));//輸出1,call()傳o對象僻造,此時this指向o中的a
(3)構(gòu)造函數(shù)調(diào)用
function Fun(name,age){
??? this.name=name;
??? this.age=age;
}
var fun=new Fun("yaoyao",24);
console.log(fun.name);//輸出yaoyao
(4)this指向----面試題
ps:? 函數(shù)自執(zhí)行? this指向window
var a=1;
var o={
??? a:2,
??? showA:function(){
??????? this.a=3;
??????? (function(){//閉包憋他,自調(diào)
??????????? console.log(this.a);//指向window中的a,輸出1
??????? })();
??????? console.log(this.a);//指向當(dāng)前對象的a,輸出3
??? }
}
o.showA();