1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script type="text/javascript"> 7 8 /** 9 * 由于函数是对象,所以可以直接把函数通过参数传递进来 10 * @param fun 函数对象 11 * @param arg 参数 12 * @returns {*} 13 */ 14 function callFun(fun , arg){ 15 //第一个参数就是函数对象 16 return fun(arg); 17 } 18 19 function say(str){ 20 alert("hello:" + str); 21 } 22 23 function sum(num){ 24 return num + 100 ; 25 } 26 27 //调用了say函数 28 callFun(say , "Leon"); //hello:Leon 29 //调用了sum函数 30 alert(callFun(sum,20)); //120 31 32 function fn1(arg){ 33 34 /** 35 * 此时返回i的是一个函数对象, 36 */ 37 var rel = function(num){ 38 console.info("inner:" + num + " arg: " + arg); 39 return arg + num ; 40 } 41 console.info("outter arg: " + arg); 42 return rel; 43 } 44 45 //此时f是一个函数对象,可以完成调用 46 var f = fn1(20); //控制台输出值: outter arg: 20 47 console.info(f); //控制台输出值: function(num) 48 console.info("-------------"); 49 alert(f(11)); //控制台输出值:inner:11 arg: 20 运行结果:31 50 51 </script> 52 53 </head> 54 <body> 55 56 </body> 57 </html>
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 7 <script type="text/javascript"> 8 9 console.info("10"+1); //111 10 //当进行减法运算时,会自动完成转换 11 console.info("10"-1); //9 12 13 //根据数字来进行排序的函数 14 function sortByNum(a,b){ 15 return a - b ; 16 } 17 18 var as = [1,2,11,33,12,190]; 19 //对应js而言,默认是按照字符串排序 20 as.sort(); 21 console.info(as); // [1, 11, 12, 190, 2, 33] 22 23 as.sort(sortByNum); 24 console.info(as); //[1, 2, 11, 12, 33, 190] 25 26 //根据数字来进行排序的函数 27 function sortByNum2(a,b){ 28 return parseInt(a) - parseInt(b) ; 29 } 30 var as2 = [1,2,"11px",33,"12px",190]; 31 as2.sort(sortByNum2); 32 console.info(as2); //[1, 2, "11px", "12px", 33, 190] 33 </script> 34 35 </head> 36 <body> 37 38 </body> 39 </html>
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 7 8 9 </head> 10 <body> 11 <div id="person"> </div> 12 <script type="text/javascript"> 13 14 function Person(name,age){ 15 this.name = name ; 16 this.age = age ; 17 } 18 19 var p1 = new Person("Leon",39); 20 var p2 = new Person("John",23); 21 var p3 = new Person("Ada",41); 22 var ps = [p1,p2,p3]; 23 ps.sort(); 24 console.info(ps); //[Person { name="Leon", age=39}, Person { name="John", age=23}, Person { name="Ada", age=41}] 25 26 /** 27 * 使用sortByName sortByAge这两个方法来处理排序,带来的我呢体是需要为每一个属性都设置一个函数 28 * 显然不灵活 29 * 但是如果通过函数的返回值调用就不一样了 30 */ 31 function sortByName(obj1 , obj2){ 32 if(obj1.name > obj2.name ){ 33 return 1; 34 }else if(obj1.name == obj2.name ){ 35 return 0 ; 36 }else{ 37 return -1 ; 38 } 39 } 40 41 ps.sort(sortByName); 42 console.info(ps); //[Person { name="Ada", age=41}, Person { name="John", age=23}, Person { name="Leon", age=39}] 43 44 function sortByAge(obj1 , obj2){ 45 return obj1.age - obj2.age; 46 } 47 ps.sort(sortByAge); 48 console.info(ps); //[Person { name="John", age=23}, Person { name="Leon", age=39}, Person { name="Ada", age=41}] 49 50 51 //改造方法 52 function sortByProperty(propertyName){ 53 var sortFun = function(obj1 , obj2){ 54 if(obj1[propertyName] > obj2[propertyName]){ 55 return 1 ; 56 }else if(obj1[propertyName] == obj2[propertyName]){ 57 return 0 ; 58 }else{ 59 return -1 ; 60 } 61 } 62 return sortFun; 63 } 64 65 ps.sort(sortByProperty("age")); 66 console.info(ps); //[Person { name="John", age=23}, Person { name="Leon", age=39}, Person { name="Ada", age=41}] 67 68 ps.sort(sortByProperty("name")); 69 console.info(ps); //[Person { name="Ada", age=41}, Person { name="John", age=23}, Person { name="Leon", age=39}] 70 71 function show(){ 72 var p = document.getElementById("person"); 73 for(var i = 0 ; i < ps.length ; i ++){ 74 p.innerHTML += ps[i].name + " , " + ps[i].age + "<br/>"; 75 } 76 } 77 78 show(); 79 </script> 80 </body> 81 </html>
时间: 2024-10-08 09:46:45