Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Override and Overload

    i am write code for overload
    PHP Code:
    // usable for ovverride
       
    Function.prototype.overload=function(newfunc)
       {
           var 
    method=this
           return function()
           {
               var 
    o=this!=window?this:null;
               var 
    x=String(newfunc).match(/((.*))/);
               if(
    x[1]){x=x[1].match(/[,]/g);if(x)x=x.length+1;else x=1;}else x=0;
               if(
    x==arguments.length)return newfunc.apply(o,arguments);else return method.apply(o,arguments);
           } 
       }
      
    //sample functions
      
    function hello(){alert("Hello")};
      
    hello=hello.overload(function (name){alert("Hello "+name)});
      
    hello=hello.overload(function (name,surName){alert("Hello "+name+" "+surName)});
    hello("cem","fırat");//output "Hello cem fırat"
    hello();//output "Hello"
    hello("cem");//output "Hello cem"
      //declare person class  
     
    function person(){}
     
    person.prototype.name="unknown",
     
    person.prototype.identity=function(name){this.name=name;},
     
    person.prototype.toString=function(){return  "identity:\n\r"+"Name: "+this.name}
     
    //declare superPerson class inherit from person and add new features
     
    function superPerson(){}
     
    //inherit all methods and fields from person
     
    superPerson.prototype=new person;
     
    //declare this class prototype
        //declera new field
     
    superPerson.prototype.surName="unknown";
        
    /*overload person.identity function(or can  will type superPerson.identity
        <because inherit all methods and fields with above code from person class> 
        */
     
    superPerson.prototype.identity=person.prototype.identity.overload(
          function(
    name,surName)
          {
             
    this.name=name;this.surName=surName;
          });
          
    //override toString method (calling person.toString and apply this object)
     
    superPerson.prototype.toString=function(){return  person.prototype.toString.apply(this)+"\n\r surName: "+this.surName};

     var 
    user=new superPerson();
     
    user.identity("murat");
     
    alert(user);
     
    /*output 
     identity:
     Name: murat
     surName: unknown*/
     
    user.identity("cem","fırat");
     
    alert(user);
     
    /*output 
     identity:
     Name: cem
     surName: fırat*/ 
    Last edited by nightwolfcem; 05-21-2009 at 08:16 AM.

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    and it does what for what reason?

  • #3
    New Coder
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    call different functions with same name(one)
    http://en.wikipedia.org/wiki/Method_overloading

  • #4
    New Coder
    Join Date
    Oct 2008
    Location
    Turkey
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    i am write new versiyon now possible have equal param count and same name functions
    Code:
       Function.prototype.overload=function(argms,newfunc)
       {
           var method=this; 
           return function()
           {
               var o=this!=window?this:null,t=true;
               if(typeof argms=="function")
               {
                   newfunc=argms; 
                   var x=String(newfunc).match(/\((.*)\)/);
                   if(x[1]){x=x[1].match(/[,]/g);if(x)x=x.length+1;else x=1;}else x=0;
                   t=x==arguments.length;
               } 
               else 
               {   
                   if(argms.length!=arguments.length)t=false;
                   else
                   for (var i=0;i<arguments.length;i++)
                   if(typeof arguments[i]!=argms[i]){t=false;break;};
               }
               if(t)return newfunc.apply(o,arguments);else return method.apply(o,arguments);
           } 
       }
    EXAMPLE
    Code:
    function hello(){alert("Hello")};
      hello=hello.overload(function (name){alert("Hello "+name)});//not necesary enter param types because there isn't any function that like param count with this function
      hello=hello.overload(["string","string"],function (name,surName){alert("Hello "+name+" "+surName)});//need param types becase we have same param count function(following)
      hello=hello.overload(["string","number"],function (name,age){alert("Hello "+name+"\n\r Your Age :"+age)});
    hello("cem","fırat");//output "Hello cem fırat"
    hello("cem",25);//out put Hello cem Your Age :25
    hello();//output "Hello"
    hello("cem");//output "Hello cem"
    hint:some variable types "array","string","function","object","number","array","boolean"
    Last edited by nightwolfcem; 05-21-2009 at 07:39 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •