I have a javascript module for creating Menu objects originally designed like this:
// original Menu module
function Menu ( ) {
alert ( "Menu ( )");
}
Menu.prototype.init = function ( ) {
alert ( "Menu.init ( )");
}
var menu = new Menu;
I now wish to wrap that inside my API like so
// new API containing Menu
( function ( $api, window, undefined ) {
$api.Menu = function ( ) {
alert ( "$api.Menu ( )");
};
$api.Menu.prototype.init = function ( ) {
alert ( "$api.Menu.init ( )");
};
}( window.$api = window.$api || {}, window ));
var menu = new $api.Menu;
It appears to work but my question is whether this is correct? eg would this end up duplicating each prototype function for each $api.Menu instance?
I ask because I've always used prototype with the first method and I'm simply unsure what Javascript is doing under the hood for the second example.