Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to avoid duplicate functions with dynamic function names

Status
Not open for further replies.

unomateo

IS-IT--Management
May 9, 2006
38
0
0
US
I have a problem with duplicate javascript functions. I am making widgets for websites.

the problem is when they add 2 widgets, they get duplicate javascript functions and the first function get all messed up.

is there a way to do something like this:

Code:
var fname = "function_name";

function fname()
{
...
}
or
Code:
var fname = "_name";

function myfunction+fname()
{
...
}
 
When you say "2 widgets" are you talking about having two copies of the same widget, or two different widgets with conflicting function names?

At any rate, I'd say you would want to approach things in a more object-oriented manner. Creating your functions as methods of an object rather than as globally declared functions will reduce the likelihood of collisions.

Code:
//Class Declaration for Widget1
function Widget1(){
 this.someValue = 5;
}
Widget1.prototype.showValue = function(){
 alert("My value is " + this.someValue);
}

//Class Declaration for Widget2
function Widget2(){
 this.someValue = 4;
}
Widget2.prototype.showValue = function(){
 alert("The value I have is " + this.someValue);
}

//Some code to look at the widgets
var firstWidget = new Widget1();
var secondWidget = new Widget2();

firstWidget.showValue();
secondWidget.showValue();

As you can see from the above example, this way you can have multiple objects with functions named the same thing, but doing different tasks. Even if you only had the one, but wanted multiple copies, it would work too - keeping the properties and methods constrained to their respective objects.
Code:
//Class Declaration for Widget1
function Widget1(){
 this.someValue = 5;
}
Widget1.prototype.showValue = function(){
 alert("My value is " + this.getValue());
}
Widget1.prototype.setValue = function(intValue){
 this.someValue = pareseInt(intValue);
}
Widget1.prototype.getValue = function(){
 return this.someValue;
}

//Some testing code
var firstWidget = new Widget1();
var secondWidget = new Widget1();

firstWidget.setValue(42);
secondWidget.setValue(100);
firstWidget.showValue();
secondWidget.showValue();

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
I'd go with dwarfthrower's recommendation of an OO approach. However, to answer your question, you can define functions with dynamic names:

Code:
var fname = 'function_name';

window[fname] = function() {
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top