自定义事件

  1. //声明事件对事
  2. var EventEmitter=require("events").EventEmitter;
  3. var event=new EventEmitter();
  4. //注册事件
  5. event.on("some_event",function(){
  6. console.log("自定义事件");
  7. });
  8. //在多久事件之后触发事件
  9. setTimeout(function(){
  10. event.emit("some_event");
  11. },3000);

附件列表

     

    课程四

    课程四

    一、函数
    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 ]

    附件列表

       

      第五课

      第五课

      一、面向对象
      1、创建对象属性
      1. var website={};
      2. website.domainName="www.806501.com";
      3. website.name="806501";
      4. website.email="kavkill@806501.com";
      5. website.age=3;
      6. website.click=3000;
      7. console.log(website);
      返回:
      { domainName: ‘www.806501.com’,
        name: ‘806501’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000 }
      2、创建对象方法
      1. var website={};
      2. website.domainName="www.806501.com";
      3. website.name="806501";
      4. website.email="kavkill@806501.com";
      5. website.age=3;
      6. website.click=3000;
      7. console.log(website);
      8. website.setDomainName=function(domainName){
      9. this.domainName=domainName;
      10. }
      11. website.getDomainName=function(){
      12. return this.domainName;
      13. }
      14. website.setDomainName("nodejs.806501.com");
      15. console.log(website.getDomainName());
      16. console.log(website);
      返回:
      { domainName: ‘www.806501.com’,
        name: ‘806501’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000 }
      nodejs.806501.com
      { domainName: ‘nodejs.806501.com’,
        name: ‘806501’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000,
        setDomainName: [Function],
        getDomainName: [Function] }
      3、另一种方式创建属性和方法
      1. var website={
      2. domainName:"www.806501.com",
      3. name:"806501",
      4. email:"kavkill@806501.com",
      5. age:3,
      6. click:3000,
      7. setDomainName:function(dn){
      8. this.domainName=dn;
      9. },
      10. getDomainName:function(){
      11. return this.domainName;
      12. },
      13. };
      14. console.log(website);
      15. website.setDomainName("nodejs.806501.com");
      16. console.log(website.getDomainName());
      返回:
      { domainName: ‘www.806501.com’,
        name: ‘806501’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000,
        setDomainName: [Function],
        getDomainName: [Function] }
      nodejs.806501.com
      4、构造函数
      1. function WebSite(domainName,name,email,age,click){
      2. this.domainName=domainName;
      3. this.name=name;
      4. this.email=email;
      5. this.age=age;
      6. this.click=click;
      7. this.setDoaminName=function(domainName){
      8. this.domainName=domainName;
      9. }
      10. this.getDomainName=function(){
      11. return this.domainName;
      12. }
      13. }
      14. var website1=new WebSite("www.806501.com","806501","kavkill@806501.com",3,3000);
      15. console.log(website1);
      16. website1.setDoaminName("nodejs.806501.com");
      17. console.log(website1.getDomainName());
      返回:
      WebSite {
        domainName: ‘www.806501.com’,
        name: ‘806501’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000,
        setDoaminName: [Function],
        getDomainName: [Function] }
      nodejs.806501.com
      5、使用prototype创建构造函数
      1. function JSWebSite(domainName,name,email,age,click){
      2. this.domainName=domainName;
      3. this.name=name;
      4. this.email=email;
      5. this.age=age;
      6. this.click=click;
      7. }
      8. JSWebSite.prototype.setDomainName=function(domainName){
      9. this.domainName=domainName;
      10. }
      11. JSWebSite.prototype.getDomainName=function(){
      12. return this.domainName;
      13. }
      14. var website=new JSWebSite("www.806501.com","kavkill","kavkill@806501.com",3,3000);
      15. console.log(website);
      16. website.setDomainName("kavkill.806501.com");
      17. console.log(website.getDomainName());
      18. console.log(website);
      返回:
      JSWebSite {
        domainName: ‘www.806501.com’,
        name: ‘kavkill’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000 }
      kavkill.806501.com
      JSWebSite {
        domainName: ‘kavkill.806501.com’,
        name: ‘kavkill’,
        email: ‘kavkill@806501.com’,
        age: 3,
        click: 3000 }

      附件列表