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

Object.keys và Object.getOwnPropertyNames

Object.keys và Object.getOwnPropertyNames

try/catch es10

An update for developer convience allows the use of try/catch without an explicit e Error reference in the catch call.

Kiểm tra định dạng email bằng Javascript

Kiểm tra định dạng email bằng Javascript

Convert Array-like to True Array

Convert Array-like to True Array

Getting the last element of a split string array

Getting the last element of a split string array