JavaScript class safe function