JavaScript Learning Note 06

Source: Internet
Author: User

function, function can return a function directly

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en"
"Http://www.w3.org/TR/html4/strict.dtd" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>js01_hello</title>
<meta name= "Author" content= "Administrator"/>
<script type= "Text/javascript" >
function sum (num1,num2) {
return num1+num2;
// }
var sum = function (num1,num2) {
return num1+num2;
}

function sum (NUM1) {
return num1+100;
// }
/**
* The space pointed to by sum has been changed from a function with two parameters to a function with only NUM1
* Only NUM1 functions are called at the time of invocation
* Special Note: function parameters and calls are not related, if the function has only one parameter, but it is passed in
* Two parameters, just match one
* So there is no overloaded function in JS
*/
var sum = function (NUM1) {
return num1+100;
}

Functions are defined in the following way
/**
* defined in the following way equals a defined
* function fn (num1,num2) {
* Alert (NUM1+NUM2);
* }
* So through the following example, a full description of the function is an object
*/
var fn = new Function ("Num1", "num2", "Alert (' Fun: ' + (NUM1+NUM2))");
FN (12,22);
Alert (sum (19));

Alert (sum (19,20));
</script>
<body>
</body>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en"
"Http://www.w3.org/TR/html4/strict.dtd" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>js01_hello</title>
<meta name= "Author" content= "Administrator"/>
<script type= "Text/javascript" >
/**
* Because the function is an object, you can pass the function directly through the parameters.
*/
function Callfun (fun,arg) {
The first argument is a function object
return Fun (ARG);
}

function sum (num) {
return num+100;
}

function say (str) {
Alert ("Hello" +str);
}
var say = xxx
Called the Say function
Callfun (say, "Leon");
The SUM function is called
Alert (Callfun (sum,20));

function Fn1 (ARG) {
/**
* A function object is returned at this time
*/
var rel = function (num) {
return arg+num;
}
return rel;
}
At this point, F is a function object that can complete the call
var f = fn1 (20);
Alert (f (20));
Alert (f (11));
</script>
<body>
</body>

function, function can directly return a function, to flexibly implement the function sort

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en"
"Http://www.w3.org/TR/html4/strict.dtd" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>js01_hello</title>
<meta name= "Author" content= "Administrator"/>

<body>
<div id= "Person" ></div>
<script type= "Text/javascript" >
/**
* Functions that are sorted according to numbers
*/
function Sortbynum (A, b) {
Return parseint (a)-parseint (b);
// }
Alert ("11" + 1);
Conversion is done automatically when subtraction occurs
Alert ("11"-1);
var as = [11px "," 12px ", 190];
For JS, the default is to sort by string.
As.sort (Sortbynum);
alert (AS);

Testing is sorted by object
function Person (name,age) {
THIS.name = name;
This.age = age;
}
var p1 = new Person ("Leno", 39);
var p2 = new Person ("John", 23);
var p3 = new Person ("Ada", 41);
var PS = [P1,P2,P3];
Ps.sort (Sortbyage);
p1.name,p1["Name"]
/**
* Use the following methods to handle sorting, the problem is that you need to set a function for each property, obviously not flexible
* But it's different if you call through the function's return value.
*/
function Sortbyname (obj1,obj2) {
if (obj1.name>obj2.name) return 1;
else if (Obj1.name==obj2.name) return 0;
else return-1;
// }
function Sortbyage (obj1,obj2) {
return obj1.age-obj2.age;
// }
//
Ps.sort (Sortbyproperty ("Age"))
function Sortbyproperty (PropertyName) {
var sortfun = function (obj1,obj2) {
if (Obj1[propertyname]>obj2[propertyname]) return 1;
else if (Obj1[propertyname]==obj2[propertyname]) return 0;
else return-1;
}
return sortfun;
}
Function Show () {
var p = document.getElementById ("person");
for (Var i=0;i<ps.length;i++) {
P.innerhtml+=ps[i].name+ "," +ps[i].age+ "<br/>";
}
}
Show ();
</script>
</body>

JavaScript Learning Note 06

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.