You already have a closure. When methodA called methodA access to a will work fine.
Object properties are different things for areas. You use scopes to implement something that behaves like "private members in other languages, but a is a local variable in the parent scope, not a member of myObjA (private or otherwise). Having a function like methodA preserves access to variables in their parent area, which means closure.
What areas that you can access are fixed: you can always access variables in your parent areas, but you call them back, and you cannot call a function with different areas before those that it had when it was defined .
Since a not a property of this , it does not matter that this not preserved when you call it. If you need to get the correct this , then yes, you will need one more work, or using another closure on myObjA :
onclick= function() { myObjA.methodA(); };
or using the # bind Function :
onclick= myObjA.methodA.bind(myObjA);
source share