Closure Function thay đổi biến
RUN
<!DOCTYPE html> <html> <head> <title>Closure trong Javascript</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script language="javascript"> // Bước 1: Tạo hàm closure function Student() { var name = ''; var age = ''; return { set : function(in_name, in_age){ name = in_name; age = in_age; }, getName : function(){ return name; }, getAge : function(){ return age; } }; } // Bước 2: khởi tạo hàm closure var studentObj = Student(); // Bước 3: Chạy hàm closure studentObj.set('Nguyễn Văn Cường', '27'); alert(studentObj.getName()); alert(studentObj.getAge()); </script> </head> <body> </body> </html>
PHÓNG TO