课程四

课程四

一、函数
1、定义函数,使用 “function 函数名”定义函数
  1. function add(domainName,name,email,age,click){
  2. var website=[];
  3. website[0]=domainName;
  4. website[1]=name;
  5. website[2]=email;
  6. website[3]=age;
  7. website[4]=click;
  8. return website;
  9. }
  10. var domainName="www.806501.com";
  11. var name="806501";
  12. var email="kavkill@806501.com";
  13. var age=3;
  14. var click=3000;
  15. var website = add(domainName,name,email,age,click);
  16. console.log(website);
返回:[ ‘www.806501.com’, ‘806501’, ‘kavkill@806501.com’, 3, 3000 ]

2、匿名函数
  1. var remove=function(domainName){
  2. return domainName;
  3. }
  4. var dn = remove("www.806501.com");
  5. console.log(dn);
返回:www.806501.com
3、回调函数
  1. function output(domainName,name,email,age,click){
  2. console.log(domainName+"|"+name+"|"+email+"|"+age+"|"+click);
  3. }
  4. function read(callback,domainName,name,email,age,click){
  5. console.log("read");
  6. callback(domainName,name,email,age,click);
  7. }
  8. read(output,"www.806501.com","8065001","kavkill@806501.com",3,3000);
返回:
read
4、嵌套函数
  1. function Website(d,n,e,a,c){
  2. var myDomainName,myName,myEmail,myAge,myClick;
  3. function set(domainName,name,email,age,click){
  4. myDomainName=domainName;
  5. myName=name;
  6. myEmail=email;
  7. myAge=age;
  8. myClick=click;
  9. }
  10. set(d,n,e,a,c);
  11. return [myDomainName,myName,myEmail,myAge,myClick];
  12. }
  13. var website=Website("www.806501.com","8065001","kavkill@806501.com",3,3000);
  14. console.log(website);
返回:[ ‘www.806501.com’, ‘8065001’, ‘kavkill@806501.com’, 3, 3000 ]

附件列表

     

    留下评论

    电子邮件地址不会被公开。 必填项已用*标注