...

View Full Version : Resolved Override and Overload



nightwolfcem
05-20-2009, 07:29 PM
i am write code for overload


// 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*/

ShadowIce
05-20-2009, 11:36 PM
and it does what for what reason? :)

nightwolfcem
05-21-2009, 12:18 AM
call different functions with same name(one)
http://en.wikipedia.org/wiki/Method_overloading

nightwolfcem
05-21-2009, 07:36 PM
i am write new versiyon :thumbsup: now possible have equal param count and same name functions


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


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"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum