/    Sign up×
Community /Pin to ProfileBookmark

Function with several instances

Hi,

I’m struggling on a JS function to generate calendars.
It works fine until I have more than one instance. Then, when I change the month in the first instance, it’s the 2nd one which makes the change.
I don’t understand very well how I should build my function.

Can you tell me what I have to do in order to get the methods applied to the right instance?

Here after is a brief extract from the way I built my function (I spare you the 500 lines).

[CODE]
function calendar(FieldId)
{
var me = this;
me.SelectedDays = Array();
//etc

GoNextMonth = function(){
//blabla
}
me.NextMonthButton = document.createElement(‘div’);
me.NextMonthButton.onclick=GoNextMonth();
}
[/CODE]

PS: I use the word ‘instance’ but my function is not a class, I call it using [I]calendar(‘test’)[/I], not [I]new calendar(‘test’)[/I])…

Cheers!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManSep 19.2007 — i actually think the whole code would be best. its highly possible that you've left out pertinent information. from what i see, i cant visualize this problem.

for instance, you pass the argument "FieldId" to the calendar function. i assume it was used someplace where you have commented out the code. however, that is a distractor for me, and prevents me from really understanding the flow of your code.
Copy linkTweet thisAlerts:
@WebnerdSep 19.2007 — You can't call your function directly. You need an instance. Otherwise, your references to "me" inside of your function will only ever update a single instance.

<i>
</i>
var newcal=new calendar(field);



You just need a better understanding of scoping.
Copy linkTweet thisAlerts:
@nababauthorSep 19.2007 — That works fine now...

http://www.babna.com/cal/

Thanks!
×

Success!

Help @nabab spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...