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

try/catch es10

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

serialize Form

Sử dụng formdata constructor để convert form, FormData, array.from(). Kết hợp window.encodeURIComponent() để encode từng value trong form.

encode/decode base64 with JS

Encoding to base64 is done with the btoa command

Object.keys và Object.getOwnPropertyNames

Object.keys và Object.getOwnPropertyNames

Làm phẳng mảng dùng phương pháp đệ quy

Làm phẳng mảng dùng phương pháp đệ quy