Method overloading check

abhijats
Tera Expert

Hi Everyone,

Last time my manager asked me to check if system support Method Overloading concept and I created a business rule with two Methods with different parameters, then I called 1st method in another business rule. but the result was not in support of Method Overloading concept. Everytime the last method was getting executing although I had created those methods with different parameters and also tried to call 1st method by its parameter only. So does that mean in SNC environment I can't use Method Overloading thing or I had performed that exercise in a wrong way. Please provide your opinion on the same, If have any sample program that will be better.

15 REPLIES 15

abhijats
Tera Expert

Hi Dale,

I am still not getting correct result always last function is overwriting the first one.

Please check the code:

function MOT (a, b, c) {
this.a = a || 2;
this.b = b || 1;
this.c = c || 0;
this.z=a+b+c;
return z;
}

function MOT (a) {
this.d = a || "hello";
this.y=d;
return y;
}

I am calling this in another rule and if I call first function with MOT(2,3,4), the result is 2. However If I put in function like MOT('work') the result is correct i.e. work.

How can I make this Concept(Method Overloading) working?


DaleHurtt
Kilo Contributor

Your functions won't work because JavaScript does not support method overloading the way you have it written. You would write the MOT function like this:


function MOT (o) {
if (o.sig) {
if (o.sig == "string") {
this.d = o.a || "hello";
this.y=d;
return this.y;
}
else {
this.a = o.a || 2;
this.b = o.b || 1;
this.c = o.c || 0;
this.z = this.a + this.b + this.c;
return this.z;
}
else {
return null;
}
}


You would then call the function like in the following examples:


x = new MOT ({sig: "string", a: "work"});
y = new MOT ({sig: "number", a: 2, b: 3, c: 4});


Note that with the way you had the functions defined, if no arguments were passed, you would have no way of knowing whether the first parameter should be defaulted to "2" or "hello". If you don't like passing an object that way and you assumed that the first parameter is always passed, and is either a non-numeric string or a number (or numeric string), you could write the function as follows:


function MOT (a, b, c) {
if (arguments.length == 0) {
return null;
}
else if (arguments.length == 1 &&
typeof a == "string") {
this.d = a;
this.y = this.d;
}
else {
this.a = a;
this.b = b || 1;
this.c = c || 0;
this.z = this.a + this.b + this.c;
}
}

x1 = new MOT (2); // x1.z returns 3;
x2 = new MOT ("Work"); // x2.y returns "Work";


One more note: if you are using "this" inside of a function, generally you are calling the function with "new myFunction ()" and creating a new object, so the function should not return any value.


abhijats
Tera Expert

Thanks Dale for such a good description, I am able to get desired output by second way of execution of Function. but first one is not running.

I have put in BR like this way:

function MOT (o) {
if (o.sig) {
if (o.sig == "string") {
this.d = o.a || "hello";
this.y=d;
return this.y;
}
else {
this.a = o.a || 2;
this.b = o.b || 1;
this.c = o.c || 0;
this.z = this.a + this.b + this.c;
return this.z;
}}
else {
return null;
}
}

x1 = new MOT ({sig: "string", a: "work"});
y1 = new MOT ({sig: "number", a: 2, b: 3, c: 4});

gs.addInfoMessage(x1.y);
gs.addInfoMessage(y1.z);

What can be the reason?


DaleHurtt
Kilo Contributor

Abhijat,

The use of the JavaScript keywords new, this, and return are all related. One of the unfortunate problems with JavaScript is that a function looks like a constructor, and vice versa. In fact, some people use them as both, but that is generally a bad idea.

When you use:


MOT ();


The keyword this is generally the global context. (In a browser, that is the variable window.) When you use:


var x = new MOT ();


The keyword this changes context, however, when MOT is called with the keyword new (i.e. used as a constructor function); it is now points to the new copy of MOT object being created. Try this simple test to show the concept. Create an HTML page with a text editor, create a script block, and in the script type the following:


var x = 3;

function MOT () {
alert (this.x);
}

MOT();
var y = new MOT ();


When you run the web page the browser will display "3" for the first invocation of MOT and display "undefined" for the second invocation of MOT. Why? The first time you call MOT, without the keyword new, the statement this.x points to the global variable x, which has a value of "3" (line 1 of the code). The second time MOT is invoked, with the keyword new, JavaScript creates a new object and assigns it to the keyword this, so this.x is undefined, as no value has been given to it.

Now, back to your code and why it does not work. When you use new MOT (), a new object is created, assigned to this and the code in the MOT function is used to construct the object (e.g. assignments of this.a = a || 2;, etc.). What you do not see is that JavaScript implicitly returns this as a result of calling new. However, because you have the statements return this.y; and return this.z; you are specifically overriding JavaScript normally returning this.

Think of it this way: when using new with a function, JavaScript automatically executes the equivalent of:


return this;


But, only if you do not override it by putting in an explicit return statement of your own. So, you code should be:


function MOT (o) {
if (o.sig) {
if (o.sig == "string") {
this.d = o.a || "hello";
this.y=d;
}
else {
this.a = o.a || 2;
this.b = o.b || 1;
this.c = o.c || 0;
this.z = this.a + this.b + this.c;
}
}
else {
return null;
}
}


One last thing: why do I return null; at the end? In this example I expect the user to either send a sig of "string" or "number". If they send something else, I don't want a valid object being returned, which the implicit return this; that JavaScripts executes would do. Perhaps a better way would be to throw an error rather than return null; that is a choice that you the programmer have to decide upon.

Note to JavaScript Gurus out there: some of what I have written is intentionally simplified (or as simplified as I can get it). I know there are a number of ways to get this to point to other objects, but as I did not think it added to the explanation, did not mention it until now. If you can think of how to describe this concept, with all of the exceptions JavaScript allows, let us know. :^)


abhijats
Tera Expert

I have understood the logic behind new,this and return. I have placed the latest code you have provided which is without 'return' :

function MOT (o) {
if (o.sig) {
if (o.sig == "string") {
this.d = o.a || "hello";
this.y=d;
}
else {
this.a = o.a || 2;
this.b = o.b || 1;
this.c = o.c || 0;
this.z = this.a + this.b + this.c;
}
}
else {
return null;
}
}

x1 = new MOT ({sig: "string", a: "work"});
y1 = new MOT ({sig: "number", a: 2, b: 3, c: 4});

gs.addInfoMessage(x1.y);
gs.addInfoMessage(y1.z);

Here, still something is not proper because it doesn't get executed.