closure javascript

tipjs,scope,global

Example closure javascript

class Example {
  constructor() {
    var _private = '';
    _private = 'private';
    this.getName = function() {return _private}
  }
}
var ex = new Example();
console.log(ex.getName()); // private
console.log(ex._private); // undefined

Lợi thế:

 Không có xung đột đặt tên 

 Không thể truy cập và sửa đổi bên ngoài 

Nhược điểm:

Logic của constructer trở nên phức tạp. 

Hàm tạo chỉ nên thực hiện khởi tạo đối tượng.

Bây giờ để thực hiện các biến riêng tư, nó phải bao gồm việc thực hiện một số phương thức và tổ chức code không rõ ràng. 

Có thể bạn đã miss một số snippets code

Đếm số phần tử trong mảng JavaScript

Đếm số lần xuất hiện của các phần tử trong mảng JavaScript

using javascript callback style error handling

using Typescript callback style error handling

group by property javascript use reduce

group by property javascript use reduce

Detect when your site is visible to users

Detect when your site is visible to users