JavaScript function encapsulation