phaserjs/phaser · error · Error
initialize must be a function
Error message
initialize must be a function
What it means
Thrown by the Phaser.Class factory when the definition object has an `initialize` key whose value is not a function. `initialize` is the reserved slot for the ES5-style constructor; Phaser requires it to be callable so it can be invoked on instantiation. Any other type (string, number, object, array, null when truthy-checked via property access) is rejected up front rather than failing later at `new`.
Source
Thrown at src/utils/Class.js:193
* }
* });
*/
function Class (definition)
{
if (!definition)
{
definition = {};
}
// The variable name here dictates what we see in Chrome debugger
var initialize;
var Extends;
if (definition.initialize)
{
if (typeof definition.initialize !== 'function')
{
throw new Error('initialize must be a function');
}
initialize = definition.initialize;
// Usually we should avoid 'delete' in V8 at all costs.
// However, its unlikely to make any performance difference
// here since we only call this on class creation (i.e. not object creation).
delete definition.initialize;
}
else if (definition.Extends)
{
var base = definition.Extends;
initialize = function ()
{
base.apply(this, arguments);
};
}View on GitHub (pinned to 41be1e462b)
Solutions
- Ensure definition.initialize is a function declaration or expression: `initialize: function () { ... }`.
- If you do not need a custom constructor, omit initialize entirely; Phaser will synthesize one (delegating to Extends if present).
- Check for typos: the key must be exactly 'initialize' (American spelling).
- If migrating from ES6 class, map `constructor` to `initialize` explicitly.
Example fix
// before
var Enemy = new Phaser.Class({
initialize: 'spawnEnemy',
hp: 10
});
// after
var Enemy = new Phaser.Class({
initialize: function (hp) { this.hp = hp; },
hp: 10
}); Defensive patterns
Strategy: type-guard
Validate before calling
function safePhaserClass(definition) {
if (definition && 'initialize' in definition && typeof definition.initialize !== 'function') {
delete definition.initialize;
}
return new Phaser.Class(definition);
} Type guard
function isValidClassDefinition(d) {
return !d || !('initialize' in d) || typeof d.initialize === 'function';
} Try / catch
try {
Klass = new Phaser.Class(definition);
} catch (e) {
if (e.message === 'initialize must be a function' && typeof definition.initialize !== 'function') {
delete definition.initialize;
Klass = new Phaser.Class(definition);
} else { throw e; }
} Prevention
- Spell the key 'initialize' (American) and assign a function expression, not a name reference.
- Run a lint rule that flags non-function values on the reserved 'initialize' key.
- If no custom constructor is needed, omit initialize entirely.
- When migrating ES6 classes, map `constructor` -> `initialize` explicitly, do not copy the class reference itself.
When it happens
Trigger: Passing `new Phaser.Class({ initialize: 'not a function' })`, `{ initialize: 42 }`, `{ initialize: { foo: 1 } }`, or `{ initialize: [function(){})] }`. Also from accidentally assigning a class reference or a config object to initialize instead of an actual function, or from a build step that serializes the definition and leaves initialize as a string.
Common situations: Refactoring a class and renaming the constructor without updating the initialize reference. Copy-pasting a class template and forgetting to replace the placeholder value. Transpiling/bundling where tree-shaking removes the function but leaves the key. Mixing ES6 class syntax (which has a real constructor) with Phaser.Class and assigning the wrong thing.
Related errors
AI-assisted analysis of phaserjs/phaser@41be1e462b (2026-08-13).
Data as JSON: /api/errors/1384bb87e2f93893.
Report an issue: GitHub.