Undefined is not a function - new manager

Status
Not open for further replies.

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
I am setting up a new manager based on the DataManager pattern.


The following works for DataManager:


DataManager.loadDatabase = function() {
...
this.loadDataFile(name, prefix + src);
...
};

DataManager.loadDataFile = function(name, src) {
...
}




but my manager gives me an Undefined is not a function error:


XRefManager.loadReferences = function() {
$dataActors.forEach(function(actor) { this.buildActorXRefs(actor); });
...
};

XRefManager.buildActorXRefs = function(actor) {
if (!actor) return;

};


The buildActorXRefs is what it's objecting to on the forEach line.


$dataActors exists and is populated at this point.


The first actor is null, but I moved the check for null actor into the forEach block and it made no difference.


My XRefManager is at the highest level, same as DataManager (not enclosed in my plugin function() {} body).


I'm hoping this will be obvious to someone else - it's sure not to me.


Edit: never mind.  Figured out I needed a .bind(this) on the function.  All working now.  Until the next issue.
 
Last edited by a moderator:

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
It's the classic javascript scope hell.


Instead of binding you could also call it this way:

Code:
XRefManager.loadReferences = function() {
    $dataActors.forEach(function(actor) { XRefManager.buildActorXRefs(actor); });
    ...
  };
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
ooh - use the 'class' name instead of this?  Interesting!  Thanks :)
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,546
Reaction score
3,717
First Language
Java's Crypt
Primarily Uses
RMMZ
Yup, since it's a "static" class (no use of prototype and instances) you can call it directy.
 

Clock Out

Veteran
Veteran
Joined
Jun 14, 2016
Messages
92
Reaction score
45
First Language
English
Primarily Uses
RMMV
Context! Each function call has its own context, A.K.A. value for this.


XRefManager.loadReferences = function() {
// Has a context named this.

$dataActors.forEach(function(actor) {
// Also has its own context named this which is different than the
// one mentioned above.
this.buildActorXRefs(actor);
});
};

XRefManager.buildActorXRefs = function(actor) {
if (!actor) return;
};


Calling XRefManager.loadReferences() causes the context of the loadReferences() function to be XRefManager. The second bit to remember is that forEach() takes a second argument, the context for the callback function. Pass the context of loadReferences() to forEach() and you're all set.


XRefManager.loadReferences = function() {
// Has a context named this.

$dataActors.forEach(function(actor) {
this.buildActorXRefs(actor);
}, this /* Passing the context of loadReferences here causes it to be the callback function's context too. */);
};

XRefManager.buildActorXRefs = function(actor) {
if (!actor) return;
};


I hope this in-depth explanation helps anyone reading this topic in the future and I highly recommend reviewing scope and context from time to time even if it's confusing. Hopefully some of it will sink in over time.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
thanks.  I kinda figured that out already, on my first post ;)
 

Clock Out

Veteran
Veteran
Joined
Jun 14, 2016
Messages
92
Reaction score
45
First Language
English
Primarily Uses
RMMV
Yeah, it's mostly to re-frame the problem and solution using JavaScript jargon for future visitors. Oh, any to remind myself how it all works since I sometimes forget, too. :blush:
 
Last edited by a moderator:

Sharm

Pixel Tile Artist
Veteran
Joined
Nov 15, 2012
Messages
12,760
Reaction score
10,884
First Language
English
Primarily Uses
N/A
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,038
Messages
1,018,466
Members
137,821
Latest member
Capterson
Top