MV Cannot read property 'apply' of undefined

Hello there, so I was doing a tutorial to learn the maker programmation, and at a moment in the tutorial than I watched. I fall on a error , in the dev tool (f8) this is the message :
TypeError: Cannot read property 'apply' of undefined
at Window_Custom (tutoMenu.js:28)
at Scene_CustomMenu.create (tutoMenu.js:14)

the tutorial :

My code :

function Scene_CustomMenu() {
this.initialize.apply(this, arguments);
}
Scene_CustomMenu.prototype = Object.create(Scene_MenuBase.prototype);
Scene_CustomMenu.prototype.constructor = Scene_CustomMenu;
Scene_CustomMenu.prototype.initialize = function() {
Scene_MenuBase.prototype.initialize.call(this);
};
Scene_CustomMenu.prototype.create = function(){
Scene_MenuBase.prototype.create.call(this);
this._customWindow = new Window_Custom(0, 0, 320, 240);
this.addWindow(this._customWindow);
}
Scene_CustomMenu.prototype.update() = function(){
if(!this.drawnWindows){
this._customWindow.drawAllItems();
this.drawnWindows = true;
}
}

function Window_Custom() {
console.log(arguments);
this.initialize.apply(this, arguments);
}
Window_Custom.prototype = Object.create(Window_Base.prototype);
Window_Custom.prototype.constructor = Window_Custom;
Window_Custom.prototype.initialize = function(x, y, width, height) {
Window_Base.prototype.initialize.call(this, x, y, width, height);
this.drawAllItems();
};

Thanks you so much if you can solve my problem.
"this._customWindow = new Window_Custom(0, 0, 320, 240);" has to be at the bottom of the script. You are trying to invoke a function that doesn't exist yet because it hasn't been declared yet. To explain the reason for this:

- if you declare a function directly with "function SomeFunction(args) {code}" it is "hoisted" to the top of the current scope, that is, it is actually declared before anything else is done.

- however, if you instead declare it via the function expression "someProperty = function(args) {code}" it will NOT be hoisted. That means, that you have to declare those functions BEFORE you use them in your script or it will do what it did when you tried testing your script.


at least, I belive that COULD be the reason for this. Because I can't see where Scene_CustomMenu is actually executed here.

Edit: okay, that wasn't the issue. I'm currently testing that and let's see if I can find it 😀

Edit 2: okay, I am stupid. It's actually pretty simple

Scene_CustomMenu.prototype.update() = function(){

the "()" after update needs to go
Oh my god I'm so stupid to don't see a mistake that big ! I have to be more aware of those things.
Thanks you my german neighbor It's working !
Don't worry about that. Stuff like that happens. But I can give you one piece of advise. Try to structure the script a bit more by adding empty lines between different functions and by using indents. Because that really helps with spotting mistakes like that and with finding stuff in general.
I'd recommend doing it similarly to how it is done in the base code.
Just to add onto what Ischmar said about proper formatting of your code - any time you post code in the forums, make sure you use the CODE tag (the </> icon on the top row). Otherwise, your indentation won't be displayed and anything in brackets may be turned into BBCode formatting.
Don't worry about that. Stuff like that happens. But I can give you one piece of advise. Try to structure the script a bit more by adding empty lines between different functions and by using indents. Because that really helps with spotting mistakes like that and with finding stuff in general.
I'd recommend doing it similarly to how it is done in the base code.
Yep that's a good idea thank you !
Just to add onto what Ischmar said about proper formatting of your code - any time you post code in the forums, make sure you use the CODE tag (the </> icon on the top row). Otherwise, your indentation won't be displayed and anything in brackets may be turned into BBCode formatting.
OK i got it !

Original thread: https://forums.rpgmakerweb.com/threads/cannot-read-property-apply-of-undefined.148570/