[js] properly extend extern classes when -D js-flatten (see #3416)