mình đang đọc cuốn The good parts đến đoạn Closure thì hơi mắc
Earlier, we made a myObject that had a value and an increment method. Suppose we
wanted to protect the value from unauthorized changes.
Instead of initializing myObject with an object literal, we will initialize myObject by
calling a function that returns an object literal. That function defines a value vari-
able. That variable is always available to the increment and getValue methods, but
the function’s scope keeps it hidden from the rest of the program:`
var myObject = function () {
var value = 0;
return {
increment: function (inc) {
value += typeof inc === 'number' ? inc : 1;
},
getValue: function () {
return value;
}
};
}();
We are not assigning a function to myObject . We are assigning the result of invoking
that function. Notice the () on the last line. The function returns an object contain-
ing two methods, and those methods continue to enjoy the privilege of access to the
value variable.`
Bác nào giải thích cho em về đoạn code bên trên cái, càng chi tiết càng tốt nha, thanks


83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?