Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Polymorphism --> (overloading) <--

Hi all,

firstly let me point out this :

http://webreference.com/programming/javascript/professional/chap3/2/3.html

now for my question - my aim is simple, i want to have an object which has 2 methods of the same name, distinguished by their parameter list.. (like we do in Java - overloading)

From the article above i got the impression that this would be possible.. maybe it is not if you know better let me know :)

Heres a simple code snippet i wrote to show you what i mean:

<html>
<head>
<title></title><script language="JavaScript">

function person () { //define class - ignore the
     this.name
     this.surname
     this.age
}
person.prototype.setPerson = setPerson;

//define methods

function setPerson (name) {
     alert("setPerson(name) called");
}

function setPerson () {
     alert("setPerson() called");
}

// ok using them now..
var myPerson = new person();

myPerson.setPerson("Mehdi");
myPerson.setPerson();

</script>

</head><body bgcolor="#ffffff" text="#000000"></body></html>

Right ? So the result im expecting is 2 alert boxes, the first one saying "setPason(name) called" , and the second saying "setPerson() called".

Thanks,
0
mehdi
Asked:
mehdi
  • 6
  • 3
  • 2
  • +2
1 Solution
 
mehdiAuthor Commented:
i just read what i posted .. let me add that the result i get is "setPerson() called" both times..

Thanks

Mehdi
0
 
AlfaNoMoreCommented:
function person () { //define class - must declare each property of this object.
    this.name       = '';
    this.surname    = '';
    this.age        = '';
    this.setPerson  = new function(X) {setPerson(X);}
    return this;
}
/* person.prototype.setPerson = setPerson; */ // what's this doing? Person is not an object yet?

function setPerson (name) {
    alert('setPerson(' + name + ') called');
}

// ok using them now..
var myPerson = new person();
myPerson.setPerson('Mehdi');

I think this is about all you can do within JavaScript. Your early-binding technique would just confuse the hell out of JavaScript.

You could have a setPersonHandler(name) function?

function setPersonHandler(name) {
    if (name) {
        setPersonNamed(name);
    }
    else {
        setPersonUnnamed();
    }
}

As the arguments are optional in JavaScript, this would be my suggestion?
0
 
CJ_SCommented:
Not really. You set setperson to be a reference to setPerson(). Polymorphism means that you have an interface. An object can implement that interface and thus it does not matter which object you instantiate since that object will always comply with the correct interface.

Basically you have two object x and y, both contain the same functions and you can instantiate either of 'm and call the function Depending on the object you'll get a different result.

Javascript does support it to some extend. You cannot use true polymorphism as you can in object oriented languages.

Second thing is that when you call the function setPerson() WITH a parameter (setPerson("Mendhi")) it does not distinguish between functions.

The only way you can get your thing to work is by adding another function and change the names of your current functions.

<html>
<head>
<title></title><script language="JavaScript">

function person () { //define class - ignore the
    this.name
    this.surname
    this.age
}
person.prototype.setPerson = setPerson;

//define methods

function setPerson()
{
   if(setPerson.arguments>0)
        setPersonX(setPerson.arguments[0]);
   else
        setPersonY();
}

function setPersonX (name) {
    alert("setPerson(name) called");
}

function setPersonY () {
    alert("setPerson() called");
}

// ok using them now..
var myPerson = new person();

myPerson.setPerson("Mehdi");
myPerson.setPerson();

</script>

</head><body bgcolor="#ffffff" text="#000000"></body></html>
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mehdiAuthor Commented:
Alfa..

person.prototype.setPerson = setPerson; is my preferred way of declaring a method thats all.

Im not interested in writing one method and testing for inputs at the moment, i'd like to be convinced that nobody knows how to do this the OO way :)

0
 
mehdiAuthor Commented:
CJ,

I disagree with much of what you said about polymorphism, overloading is not necessarily to do with interfaces at all.. Its a far simpler kitty..

I like your example, its a slightly nicer way of doing things.. There was a little error though - you missed .length out from the arguments :

if(setPerson.arguments.length>0) { ...

This is the best suggestion so far - im gonna give it a bit of time to see if anyone knows how to overload without programatic intervention.

"You cannot use true polymorphism as you can in object oriented languages." - i think you are right.


Thanks,

Mehdi
0
 
CJ_SCommented:
a little mistake yea :-/ not tested either :-/

Polymorphism IS the calling of a function using one and the same interface. My explanation was correct, the interpretation might be wrong.

CJ
0
 
knightEknightCommented:
In javascript there is no actual overloading of methods, but you can mimick different method calls like this:

function setPerson(name,age)
{
   if ( name == undefined  &&  age == undefined )
      alert("setPerson() called");
   if ( age == undefined )
      alert("setPerson(name) called");
   else
      alert("setPerson(name,age) called");
}

0
 
knightEknightCommented:
oops, left out an "else"
0
 
AstromanCommented:
> Alfa..

> person.prototype.setPerson = setPerson; is my preferred > way of declaring a method thats all.

> Im not interested in writing one method and testing for > inputs at the moment, i'd like to be convinced
> that nobody knows how to do this the OO way :)


This is more efficient too, because the function is stored in the prototype, whereas doing the:

function person () { //define class - must declare each property of this object.
   this.name       = '';
   this.surname    = '';
   this.age        = '';
   this.setPerson  = new function(X) {setPerson(X);}
   return this;
}

method creates a copy of the function for every instantiated object.

AlphaNoMore:
"/* person.prototype.setPerson = setPerson; */ // what's this doing? Person is not an object yet?"

Most JavaScript implementations attach a prototype object to every function, in case it used as a constructor ( you can force this by creating an object and then discarding it). Properties of the prototype appear in all objects created using it, You can even add properties to a prototype at run time, which can then be seen by all the instantiated objects.


As far as I know, variadic functions are the only way to achieve polymorphic behaviour in JavaScript.
0
 
mehdiAuthor Commented:
Its looking unanimous,

points go to CJ guys,

CJ, The way you originaly spoke of "objects implementing interfaces" threw me off completely (implementing an interface to me means something distincly different to writing an overloading method..)

- sounded like you were talking about Danish blue cheese or something :p

Thanks all, my suspitions have been confirmed.

M
0
 
mehdiAuthor Commented:
not quite what i had in mind, but this seems to be as close as JavaScript comes.

0
 
CJ_SCommented:
:-) I'm sorry, but that talk kind of came from VB where interfaces are actual keywords... and interfaces have a meaning to me. Exactly that.

I am sorry that you did not find a totally satisfying answer though...

CJ
0
 
mehdiAuthor Commented:
Well, im from a java background.. i am familiar with interfaces.. maybe (probabaly) somewhat different to VB's dunno..

Your answer was fine - There doesnt appear to be a true blue method of doing it.. (like in java) - but then your sugestion of  setPerson.arguments was new to me, and is probably what i'll be using in future - not what i call overloading - but a good answer.

-Med
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now