How to inherit custom javascript from parent to child pages?

Just define the function in its own separate JS file, and load that file alongside the page-specific files. The “WordPress way” would be to register the script containing the shared function(s) and then set it as a dependency of the other scripts:

wp_register_script( 'my-functions', get_theme_file_uri( 'js/functions.js' ) );

if ( is_page( 1 ) ) {
    wp_enqueue_script( 'my-page-1', get_theme_file_uri( 'js/page-1.js' ), [ 'my-functions' ] );
}

if ( is_page( 2 ) ) {
    wp_enqueue_script( 'my-page-2', get_theme_file_uri( 'js/page-2.js' ), [ 'my-functions' ] );
}

// etc.