ml5js uses a utility called handleArguments to allow developers to pass arguments to model constructors or methods in any order. Instead of strictly following a positional argument pattern, the library inspects the type of each argument provided and maps it to a named property.
Argument Mapping Logic
When you pass arguments, they are categorized as follows:
- string: Assigned to
string (often used for model names or URLs). - number: Assigned to
number. - function: Assigned to
callback. - Array: Assigned to
array. - HTMLVideoElement: Assigned to both
video and audio. - HTMLImageElement / HTMLCanvasElement / ImageData / tf.Tensor3D: Assigned to
image. - Other Objects: Any object that doesn't match the media types above is treated as an
options object.
Usage Pattern
Because the library identifies arguments by type, you can often omit arguments or change their order. However, if you provide multiple arguments of the same type (e.g., two different objects), the last one provided will override the previous ones.
Example of flexible argument passing:
// Instead of: model.predict(image, options, callback)
// You can do:
model.predict(callback, image, options);