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