JavaScript經(jīng)典學習筆記總結_第1頁
JavaScript經(jīng)典學習筆記總結_第2頁
JavaScript經(jīng)典學習筆記總結_第3頁
JavaScript經(jīng)典學習筆記總結_第4頁
JavaScript經(jīng)典學習筆記總結_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

0、在Javascript中沒有函數(shù)(重載)的概念1、在js中為某個對象(控件)綁定事件通??梢圆扇煞N手段: 1》<inputtype=”input”onclick=”clickHandler()”> 2><inputtype=”button”id=”button1”> <scripttype=”text/javascript” Varv=document.getElementById(“button1”); v.onclick=clickHandler; </script>2、有兩種類型的Cookie 1>持久性Cookie,會被存儲到客戶端的硬盤上。 2>會話Cookie,不會存儲到客戶端的硬盤上,而是放在瀏覽器進程所處的內(nèi)存當中,當瀏覽器關閉則該會話Cookie就銷毀了。3、在JavaScript中,函數(shù)(function)就是對象。4、在JavaScript中有一個Function對象,所有自定義的函數(shù)都是Function對象類型的。Function對象接收的所有參數(shù)都是字符串類型的,其中最后的一個參數(shù)就是要執(zhí)行的函數(shù)體,而前面的參數(shù)則是函數(shù)真正需要接收的參數(shù)。varadd=newFunction(“number”,”number1”,”alert(number+nubmer1)”);add本身是Function函數(shù)對象的引用。 varadd=newFunction("m","n","varo=newPerson('qqqqq','wwww');o.sayHello();"); add("qweqwe","123123"); functionPerson(username,password){ this.username=username; this.password=password; Ptotype.sayHello=function(){ alert(this.username+""+this.password); } }5、在JavaScript中,每個函數(shù)都有一個隱含的對象arguments,表示給函數(shù)實際傳遞的參數(shù)。 arguments.length,length屬性表示實際給函數(shù)傳遞的參數(shù)個數(shù)。 functionadd(){ if(1==arguments.length){ alert(arguments[0]);}elseif(2==arguments.length){ alert(agruments[0]+agruments[1]);}}6、每個函數(shù)對象都有一個length屬性,表示該函數(shù)期望接收的參數(shù)個數(shù)。他與函數(shù)的arguments不同,arguments表示函數(shù)實際接收的參數(shù)個數(shù),函數(shù)名.length期望接收參數(shù)的個數(shù)。7、JavaScript中有五種原始數(shù)據(jù)類型:Undefined,Null,Boolean,Number以及String8、Undefined數(shù)據(jù)類型的值只有一個:undefined。9、Null數(shù)據(jù)類型的值只有一個:null。10、Boolean數(shù)據(jù)類型的值有兩個:true和false11、JavaScript中沒有char字符數(shù)據(jù)類型,所以單引號和雙引號都表示字符串數(shù)據(jù)類型12、typeof是一元運算符,后跟變量的名稱,用于獲取變量的數(shù)據(jù)類型,其返回值有5個:undefined、boolean、number、string以及object。驗證變量是否為原始數(shù)據(jù)類型vars=“hello”;//s是原始數(shù)據(jù)類型alert(typeofs);vars1=newString(“123”);//對象數(shù)據(jù)類型 alert(typeofs1);13、在JavaScript中,如果函數(shù)沒有聲明返回值,那么會返回undefined。14、null與undefined的關系:undefined實際上是從null派生出來的。15、強制類型轉換:在JavaScript中有3中強制類型轉換,Boolean(value)、Number(value)、String(value)。vars=String(“hello”);alert(typeofs);//string16、在JavaScript中,對于函數(shù)中定義的變量來說,加var表示局部變量,不加var表示的是全局變量。17、在JavaScript中,所有的對象都是從Object對象繼承來的。Object中的屬性是不可枚舉的,因此無法通過for……in語句得到其中的屬性。 varobject=newObject(); for(varIinobject){ alert(i);}alert(pertyIsEnumerable(“prototype”));//判斷對象是否有枚舉的屬性。JavaScript中最頂層的對象是window對象,瀏覽器窗口對象。for(varvinwindow){ alert(v);//打印window的所有屬性。}18、在Javascript中,可以動態(tài)添加對象屬性,也可以動態(tài)刪除對象屬性。varobject=newObject();alert(object.username);//undefined//如果給對象添加屬性并賦值共有兩種方式(.和[“”])。注意下::::object.username=“shengsiyuan”;object[“username”]=”shengsiyuan”;//有點像EL表達式。alert(object.username);//如果想要從對象中刪除執(zhí)行的屬性,如下deleteobject.username;//username屬性從object對象中被刪除。alert(object.username);在JavaScript中定義對象的最常見的方式,也是開發(fā)中常用的。 varobject={username:“zhangsan”,password:”123”}; alert(object.username+object.password);小例子:Jquery.ajax({ url:elem.a,aysnc:false});19、在JavaScript中,數(shù)組中的sort方法來說,他會將待排序的內(nèi)容轉換為字符串(調用toString()方法),按照字符串的先后順序進行排序。 vararray=newArray(); array.push(1);array.push(2);alert(array.length);最常用如下:vararray=[1,3,25];array.sort();//如上說明,內(nèi)部是轉換為字符串排序的。alert(array);functioncompare(num,num1){ vartemp=parentInt(num); vartemp1=parentInt(num1); if(temp<temp1){ return-1;}elseif(temp==temp1){ Return0;}elseif(temp>temp1){ Return2;}}array.sort(compare);//排序函數(shù)對象的引用。alert(array);//匿名函數(shù) function(){}20、JavaScript中定義對象的幾種方式(JavaScript中沒有類得概念,只有對象) 1》基于已有對象擴充其屬性和方法 varobject=newObject(); ="zhangsan"; object.sayName=function(name){ =name; alert();} object.sayName('lisi');object.sayName(“l(fā)isi”);2>工廠方式}//工廠方式創(chuàng)建對象functioncreateObject(){ varobject=newObject(); object.username=“zhangsan” object.password=“123”; object.get=function(){ alert(this.username+“,”+this.password); } returnobject;varobject1=newObject();varobject2=newObject();object1.get();讓一個函數(shù)對象被多個對象所共享,而不是每個對象擁有一個函數(shù)對象 functionget(){ alert(this.username+this.password);}functioncreateObject(username,password){ varobject=newObject(); object.username=username; object.password=password; object.get=get; returnobject;}varobject=createObject("z","123");varobject2=createObject("x","123");object.get();object2.get();3》構造函數(shù)無參數(shù)或者帶參數(shù)functionPerson(username,password){ //在執(zhí)行第一行代碼之前,js引擎會為我們生成一個對象 this.username=username; this.password=password; this.getInfo=function(){ alert(this.username+“,”+this.password);}//此處會有一個隱藏的return語句,用于將之前生成的對象返回。}4》使用Prototype(原型定義)創(chuàng)建對象 functionPerson(){ } Ptotype.username=“zhangsan”; Ptotype.password=“123”; Ptotype.getInfo=function(){ Alert(this.username+“,”+this.password); } varperson=newPerson();varperson1=newPerson();person.username=“l(fā)isi”;prson.getInfo();prson1.getInfo();單純使用原型方式定義類無法在構造函數(shù)中為屬性賦初值,只能在對象那個生成后再去改變屬性值。如果使用原型方式對象,那么生成的所有對象共享原型中的屬性,這樣一個對象改變了該屬性也會反映到其他對象中。Array例子P1Person() username=newArray();共享同一個引用的值。P2 Person()password

不錯的解決方案如下://使用原型方式+構造函數(shù)方式來定義對象,每個對象之間的屬性互不干擾,兩個對象之間共享同一個方法。functionPerson(){this.username=newArray();this.password=“123”;

}Ptotype.getInfo=function(){alert(this.username+“,”+this.password);}varperson=newPerson();varperson1=newPerson();person.username.push(“zhangsan”);person.username.push(“l(fā)isi”);person.getInfo();person1.getInfo();5》動態(tài)原型方式定義對象在構造函數(shù)總通過標志量讓所以對象共享一個方法,而每個對象擁有自己的屬性。FunctionPerson(){This.username=“zhangsan”;This.password=“123”;If(typeofPerson.flag==“undefined”){Alert(“invoked”);Ptotype.getInfo=function(){Alert(this.username+this.password);}Person.flag=true;}}Varp=newPerson();Varp2=newPerson();p.getInfo();p2.getInfo();21、JavaScript中的繼承1》對象冒充functionParent(username){this.username=username;this.sayHello=function(){alert(this.username);}}functionChild(username,password){//下面三行代碼是最關鍵的代碼this.method=Parent;//定義method屬性,將Parent函數(shù)指定給methodthis.method(username);//Parent函數(shù)中的this指的是該Child對象deletethis.method;this.password=password;this.sayWorld=function(){alert(this.password);}}varparent=newParent(“zhangsan”);varchild=newChild(“l(fā)isi”,”123”);parent.sayHello();child.sayHello();child.sayWorld();2》call調用方式Call方法是Function對象中的方法,因此我們定義的每個函數(shù)都擁有該方法。可以通過函數(shù)名來調用call方法,call方法的第一個參數(shù)會被傳遞給函數(shù)中的this,從第2個參數(shù)開始,逐一賦值給函數(shù)中的參數(shù)。//繼承的第二種方式,call方法方式,F(xiàn)unction對象中的方法。functiontest(str){alertI(this.username+str);}Varo=newObject();o.username=“zhangsan”;test.call(o,”xxx”);3》使用appl

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論