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

promise in javascript

promise in javascript

Extend Object javascript

Extend Object javascript

closure javascript

closure javascript

Async await in javascript

Async await in javascript

Rest Operator for Objects

Now we can use rest on the properties of an Object. It allows us to explicitly extract certain named variables, and assign any uncalled variables into a catchall Object.