<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JavaScript Study 2015.11.9--</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> </style> <script type="text/javascript"> //function的定義 /* function是對象,可是與對象又有很大的不一樣。 */ function fn1(){ alert("fn1"); } var fn2 = fn1;//fn1,fn2拷貝內存空間 //alert(fn1); //alert(fn2); fn2 = function(){ alert("fn2"); } //fn1();//fn1 //fn2();//fn2 var obj1 = new Object(); obj1.name = "obj1"; var obj2 = obj1;//obj1,obj2指向內存中的同一塊空間 obj2.name = "obj2"; document.write(obj1.name);//obj2 document.write("<br>"); document.write(obj2.name);//obj2 document.write("<br>"); //function是對象,因此,能夠做爲function的參數進行傳遞 function callFun(funName,arg){ funName(arg); } function write(arg){ document.write(arg); document.write("<br>"); } callFun(write,"function is a arg");//write是function,這裏做爲參數。 //Sort var as = [1,2,30,22,12,90,80]; document.write(as.sort()); //按字符排序 document.write("<br>"); //怎麼按數字排序呢 function sortByNum(a,b){ return a-b; } document.write(as.sort(sortByNum)); //按數字排序 document.write("<br>"); //怎麼對對象排序呢 function Person(name,age){ this.name = name; this.age = age; } var p1 = new Person("adada",30); var p2 = new Person("disk",18); var p3 = new Person("bob",20); var p4 = new Person("harhar",50); as = [p1,p2,p3,p4]; as.sort(); for(var i=0;i<as.length;i++){ document.write(as[i].name+","+as[i].age); document.write("<br>"); } document.write("<br>"); function sortByName(obj1,obj2){ if(obj1.name>obj2.name){ return 1; } else if (obj1.name<obj2.name) { return -1; } else return 0; } as.sort(sortByName); for(var i=0;i<as.length;i++){ document.write(as[i].name+","+as[i].age); document.write("<br>"); } function sortByAge(obj1,obj2){ return obj1.age - obj2.age; } as.sort(sortByAge); for(var i=0;i<as.length;i++){ document.write(as[i].name+","+as[i].age); document.write("<br>"); } //若是Person有n個屬性,那就須要些n個方法。 //通用的實現方式: function sortByPropertyName(propertyName){ //這裏返回了一個function return function(obj1,obj2){ if(obj1[propertyName]>obj2[propertyName]){ return 1; }else if(obj1[propertyName]<obj2[propertyName]){ return -1; } else return 0; } } as.sort(sortByPropertyName("name"));//只須要傳入屬性名就能夠了 for(var i=0;i<as.length;i++){ document.write(as[i].name+","+as[i].age); document.write("<br>"); } as.sort(sortByPropertyName("age"));//只須要傳入屬性名就能夠了 for(var i=0;i<as.length;i++){ document.write(as[i].name+","+as[i].age); document.write("<br>"); } function sum(num1,num2){ document.write(num1+num2); document.write("<br>"); } //上面的語句= var sum = function(num1,num2){ document.write(num1+num2); document.write("<br>"); } function sum(num1){ document.write(num1+100); document.write("<br>"); } //上面的語句= sum=function(num1){ document.write(num1+100); document.write("<br>"); } //會覆蓋 sum(20);//120 sum(20,20);//120 //arguments function args(a1,a2,a3){ document.write(arguments.length); document.write("<br>"); } args(1); args(1,2); args(1,2,3); args(1,2,3,4);//這裏並不會報錯,而是超出的部分不使用 ///1, 函數的內存圖,與對象的區別。 // 2, Function的Sort,對象的Sort。 //3, callee apply //4, json //5, factory createPerson //6, prototype的內存圖。 //7, prototype重寫的內存圖。 </script> </head> <body> <div id="wrap"> </div> </body> </html>