Some code cleanup in javascript and python

change patern that are prone to error, like function redifinition
and other.