Quirrel4.41.0

types.Class.instance

Method on every class
class.instance(): any

Creates a new instance of the class without running its constructor.

examples/types/class/instance-basic.nut
class Turret { active = false constructor() { this.active = true } }
let blueprint = Turret.instance()
println("blueprint.active =", blueprint.active)
Output:
blueprint.active = false

Return value

A new instance whose fields hold the class's plain declared defaults - the same values ClassName() would give them before the constructor runs, since Quirrel never clones a field's default value per instance (see types.Class.newmember's Notes on default values, and the language reference on class instances).

Notes

Locks the class as a side effect, the same as calling ClassName(...) does: a class permanently stops accepting new plain fields the first time any instance of it exists, whichever way that instance was made.

Useful for a class whose constructor requires arguments a caller does not have yet, or for tooling that wants a template instance to inspect without running arbitrary constructor code.

Example

examples/types/class/instance.nut
class Widget {
  ready = false
  constructor() { this.ready = true }
}
let viaCall = Widget()
let viaInstance = Widget.instance()
println("viaCall.ready =", viaCall.ready)
println("viaInstance.ready =", viaInstance.ready)
Output:
viaCall.ready = true
viaInstance.ready = false

See also

callCreates a new instance of the class and runs its constructor, the same as writing ClassName(...). Exists so a class value obtained indirectly (through a variable, a table slot, or getclass()) can still be instantiated through a uniform .call(...) spelling shared with functions.
getclassReturns the class this instance was constructed from.
lockLocks the class immediately, without creating an instance.
classclass index