Closures vs. classes for encapsulation?
问题 I'm new to JS (from C++/etc), and it's just occurred to me that closures seem to be a simpler and more convenient way to handle encapsulation than classes. This code seems to give a simple way to handle encapsulation: function addProperty(o) { var value; o["get"] = function() { return value; } o["set"] = function(v) { value = v; } } // create two independent objects which are instances of pseudo-class 'addProperty', // which each have their own version of a set of local fields, and methods