Lặn sâu JavaScript

Vì tất cả Phần mềm đều dựa trên Web nên JavaScript thống trị web. Mọi phần mềm dựa trên Web đang ngày càng phụ thuộc nhiều hơn vào JavaScript

Tìm hiểu cách viết mã bằng một trong những ngôn ngữ được yêu cầu nhiều nhất hiện nay

Bàn thắng

  • Công cụ JavaScript
  • Các loại và biến
  • Câu điều kiện
  • Toán Tử Và Biểu Thức
  • Vòng lặp và Mảng
  • Chức năng và Sử dụng Đối tượng
  • Mô hình đối tượng tài liệu (DOM)
  • Hoạt động DOM
  • Mô hình sự kiện
  • Giới thiệu về JQuery
  • Plugin JQuery
  • Mẫu HTML5 có tay lái
  • Hiệu suất DOM
  • Hàm Khai báo, Biểu thức và IIFE
  • Đóng cửa và phạm vi
  • mô-đun và mẫu
  • Lớp và Constructor
  • Phương thức, Thuộc tính và Kế thừa
  • Lời hứa và lập trình không đồng bộ
  • Thử nghiệm
  • Thử nghiệm đơn vị - Gián điệp và chế giễu với Sinon

điều kiện tiên quyết

  • Một máy tính có Internet
  • Kiến thức cơ bản về Java

Tài liệu này được viết để giúp các nhà phát triển JavaScript hiểu sâu về các phần kỳ lạ của JavaScript và để chuẩn bị cho các cuộc phỏng vấn, các tài nguyên sau thực sự hữu ích để viết tài liệu này

  • Đặc tả ngôn ngữ ECMAScript 6
  • MDN
  • Hướng dẫn JavaScript hiện đại
Ghi chú chung
  • Không cần thuộc tính nào cho phần tử
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    7
  • Lợi ích của một tệp riêng biệt là bộ nhớ đệm của trình duyệt
  • Nếu thuộc tính
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    8 được đặt, nội dung sẽ bị bỏ qua
  • JavaScript diễn giải ngắt dòng dưới dạng dấu chấm phẩy ngầm (không phải lúc nào cũng vậy)
Khái niệm và thuật ngữ

Bối cảnh thực hiện

  • Bối cảnh thực thi hoạt động hợp lý hình thành ngăn xếp cuộc gọi
  • Bối cảnh thực thi hàng đầu trên ngăn xếp cuộc gọi là bối cảnh thực thi đang chạy
  • Trình thông dịch bắt đầu bằng bối cảnh thực thi toàn cầu
  • Mỗi lần gọi một hàm, sẽ tạo một bối cảnh thực thi mới được thêm vào đầu ngăn xếp thực thi
  • Giải thích cấp thấp
    • Đột nhập vào
      • Từ vựngMôi trường
        • Là một môi trường từ vựng
        • Giữ các khai báo
          function wrapper() {
            return printArgs.apply(this, arguments);
          }
          9 và
          Array.prototype.slice.call(arguments);
          0
        • Array.prototype.slice.call(arguments);
          1 tài liệu tham khảo
      • BiếnMôi trường
        • Là một môi trường từ vựng
        • Giữ các ràng buộc được tạo bởi
          Array.prototype.slice.call(arguments);
          2 và
          Array.prototype.slice.call(arguments);
          3
        • Array.prototype.slice.call(arguments);
          1 tài liệu tham khảo
      • Ràng buộc này
        • Giá trị được liên kết với từ khóa
          Array.prototype.slice.call(arguments);
          5
  • Giải thích cấp cao
    • Có thể chia thành
      • giai đoạn sáng tạo
        • Tạo một đối tượng biến
          • khai báo biến
          • khai báo hàm
          • Tranh luận
        • Tạo chuỗi phạm vi
        • Xác định giá trị của
          Array.prototype.slice.call(arguments);
          5
      • Giai đoạn thực hiện
        • Mã được giải thích và thực thi
  • Môi trường từ vựng
    • Là một loại đặc điểm kỹ thuật
    • Giống như
      Array.prototype.slice.call(arguments);
      7 và
      Array.prototype.slice.call(arguments);
      8
    • Bao gồm
      • Array.prototype.slice.call(arguments);
        9. ràng buộc định danh
      • function User(name) {
        
          // Not called with new
          if (!new.target) {
            return new User(name);
          }
        
          this.name = name;
          
        }
        0 tham chiếu đến môi trường từ vựng bên ngoài hoặc
        function User(name) {
        
          // Not called with new
          if (!new.target) {
            return new User(name);
          }
        
          this.name = name;
          
        }
        1

Định nghĩa bài văn

  • Là giá trị của
    Array.prototype.slice.call(arguments);
    5 tham chiếu đến đối tượng sở hữu mã thực thi hiện tại
  • Được xác định bởi cách một chức năng được gọi

Phạm vi

  • Vùng nơi liên kết giữa một tên (như tên biến) với một thực thể là hợp lệ

Khép kín

  • Bao đóng là một hàm ghi nhớ các biến bên ngoài của nó và có thể truy cập chúng
  • Sự kết hợp của một hàm và môi trường từ vựng trong đó hàm đó được khai báo
  • function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    3 chính là đối tượng chức năng
  • Truy cập các biến bên ngoài phạm vi từ vựng ngay lập tức tạo ra một bao đóng
  • Xảy ra khi chúng ta có một chức năng lồng nhau
  • Công cụ JavaScript cũng có thể tối ưu hóa, loại bỏ các biến không được sử dụng để tiết kiệm bộ nhớ
  • Một đối tượng
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    4 sống trong
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    5 miễn là có một chức năng có thể sử dụng nó. Và khi không có gì, nó sẽ bị xóa
  • Tất cả các chức năng trong JavaScript là bao đóng
  • Thuộc tính bên trong
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    6 của hàm, đề cập đến môi trường từ vựng bên ngoài

IIFE

  • Biểu thức hàm được gọi ngay lập tức
  • Một mẫu thiết kế được hầu hết các thư viện phổ biến sử dụng để đặt tất cả mã thư viện bên trong phạm vi cục bộ
  • Không có thuộc tính chung nào được tạo cho hàm (biểu thức hàm ẩn danh)
  • Tất cả các thuộc tính được tạo bên trong biểu thức hàm đều nằm trong phạm vi cục bộ
  • Đóng gói, duy trì không gian tên chung vì bất kỳ biến nào được khai báo trong thân hàm sẽ là cục bộ của bao đóng nhưng sẽ vẫn tồn tại trong suốt thời gian chạy
  • Những lợi ích
    • phạm vi cục bộ

cẩu

  • Khai báo biến và hàm được đưa vào bộ nhớ trong giai đoạn biên dịch
  • Giữ chính xác nơi bạn đã nhập mã trong mã của mình (không thực sự được chuyển lên trên cùng)
  • Chỉ khai báo tời, không khởi tạo
  • Tuyên bố góp phần vào
    Array.prototype.slice.call(arguments);
    8 khi phạm vi thực hiện được nhập (^)

Gọi lại

  • Hàm gọi lại là một hàm được truyền vào một hàm khác dưới dạng đối số
  • Được gọi bên trong hàm được gọi tại một thời điểm nào đó, chẳng hạn như sau khi hoàn thành thao tác không đồng bộ

địa ngục gọi lại

  • Gọi lại lồng sâu
  • Một ví dụ về
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    8

Lời hứa

  • Đại diện cho một giá trị có sẵn hoặc sẽ có sẵn sau này
  • Đại diện cho việc hoàn thành hoặc thất bại của một hoạt động bị trì hoãn hoặc không đồng bộ và giá trị kết quả của nó
  • Hàm được truyền với các đối số
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    9 và
    var user = new function() {
      this.name = 'Anas';
      this.age = 'Ali';
    }
    0 được gọi là hàm
    var user = new function() {
      this.name = 'Anas';
      this.age = 'Ali';
    }
    1
  • Có thể là
    • Hoàn thành với một giá trị
    • Bị từ chối có lý do (lỗi)
  • Thuộc tính bên trong của các trường hợp lời hứa
    • var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      2 giá trị hoặc lỗi
    • var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      3 có thể.
      var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      4,
      var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      5 hoặc
      var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      6
    • Hàng đợi
      var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      7 dành cho trình xử lý
      var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      8
    • Hàng đợi
      var user = new function() {
        this.name = 'Anas';
        this.age = 'Ali';
      }
      9 cho trình xử lý
      Object.create(proto[, descriptors])
      0
  • phương pháp
    • Object.create(proto[, descriptors])
      1
    • Object.create(proto[, descriptors])
      2
    • Object.create(proto[, descriptors])
      3 trả về một lời hứa đã giải quyết với giá trị đã cho
    • Object.create(proto[, descriptors])
      4 trả lại một lời hứa bị từ chối với lỗi

polyfill

  • Các tập lệnh lấp đầy khoảng trống và bổ sung các triển khai còn thiếu cho JavaScript hoặc sửa đổi các triển khai hiện có để hỗ trợ tiêu chuẩn hiện đại
  • Sử dụng cùng một API
  • Giống như pollyfill cho ________ 33 _____5 không được hỗ trợ trong ________ 33 _______6 hoặc cho những lời hứa
  • Hoặc thăm dò các phương thức mới trong ES6 sẽ được sử dụng trong tất cả các trình duyệt
  • ví dụ
    • Babel polyfill
    • polyfill. io

dự phòng

  • Thay thế tính năng bằng
    • chức năng đơn giản hóa
    • Hoặc plugin của bên thứ ba
    • Hoặc thông báo lỗi
  • Like nếu trình duyệt không hỗ trợ tag video thì thay bằng flash plugin

ghi chú

  • Mọi lời gọi hàm đều có cả phạm vi và ngữ cảnh được liên kết với nó
  • Khi một bối cảnh thực thi được tạo, các thành phần
    Array.prototype.slice.call(arguments);
    7 và
    Array.prototype.slice.call(arguments);
    8 của nó ban đầu có cùng giá trị
  • Giá trị của thành phần
    Array.prototype.slice.call(arguments);
    8 không bao giờ thay đổi
  • Giá trị của thành phần
    Array.prototype.slice.call(arguments);
    7 có thể thay đổi trong quá trình thực thi mã trong ngữ cảnh thực thi như khi nhập một khối
  • Mỗi lần chạy cho một khối vòng lặp có một
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    4 riêng biệt

Ví dụ về lời hứa

function task1() {

  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log('Task1 is completed');
      resolve();
    }, 1100);
  });
  
}

function task2() {

  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log('Task2 is completed');
      resolve();
    }, 1200);
  });
  
}

function task3() {

  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log('Task3 is completed');
      resolve();
    }, 1300);
  });
  
}

task1()
  .then(task2)
  .then(task3);

Liên kết polyfill

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}

thủ thuật chức năng

Người trang trí

Là một trình bao bọc xung quanh một chức năng làm thay đổi hành vi của nó. Giống như lưu trữ kết quả hoặc định dạng chúng

Để thực hiện chúng, chúng ta cần

  • var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    2 cuộc gọi
    var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    3 với ngữ cảnh và lập luận nhất định
  • var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    4 cuộc gọi
    var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    3 với ngữ cảnh và đối số đã cho được truyền dưới dạng giống như mảng
  • var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    6 trả về một
    var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    7 của hàm
    var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    3 để sửa ngữ cảnh và các đối số đầu tiên nếu được cung cấp

Ghi chú

  • var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
    9 thực sự hữu ích với
    • Đặt bối cảnh cho chức năng gọi lại của
      Object.create(null)
      0
    • chức năng một phần
    • chức năng cà ri

chức năng một phần

Tạo một chức năng mới bằng cách sửa một số tham số của chức năng hiện có

chức năng cà ri

  • Dịch một chức năng từ có thể gọi là
    Object.create(null)
    1 thành có thể gọi là
    Object.create(null)
    2
  • Để cho phép nó được gọi bình thường hoặc nhận các phần

Chuyển hướng cuộc gọi

Hàm bao bọc chuyển mọi thứ mà nó nhận được sang một hàm khác

function wrapper() {
  return printArgs.apply(this, arguments);
}

Phương thức mượn

Sử dụng một phương thức từ một đối tượng khác trên đối tượng của chúng tôi

Array.prototype.slice.call(arguments);

Mục tiêu
  • Các đối tượng là các mảng kết hợp, lưu trữ các cặp khóa-giá trị
  • Khóa phải là
    Object.create(null)
    3 hoặc
    Object.create(null)
    4, giá trị có thể là bất kỳ giá trị nào
  • Để truy cập một thuộc tính, chúng ta có thể sử dụng
    • Ký hiệu dấu chấm.
      Object.create(null)
      5
    • Ký hiệu ngoặc vuông
      Object.create(null)
      6
  • Một đối tượng trống có thể được tạo bằng cách sử dụng
    • Sử dụng hàm tạo đối tượng
      Object.create(null)
      7
    • Sử dụng đối tượng theo nghĩa đen
      Object.create(null)
      8
  • Để loại bỏ một thuộc tính, chúng ta có thể sử dụng toán tử
    Object.create(null)
    9
  • kiểm tra tồn tại
    • Sử dụng
      function User(firstName, lastName) {
      
        var self = this;
      
        this.firstName = firstName;
        this.lastName = lastName;
      
        function getFirstName() {
          return self.firstName;
        }
      
        function getLastName() {
          return self.lastName;
        }
      
        this.getFullName = function() {
          return getFirstName() + ' ' + getLastName();
        }
      
      }
      
      var user = new User('Ahmad', 'Fares');
      
      console.log(user.getFullName());
      0,
      function User(firstName, lastName) {
      
        var self = this;
      
        this.firstName = firstName;
        this.lastName = lastName;
      
        function getFirstName() {
          return self.firstName;
        }
      
        function getLastName() {
          return self.lastName;
        }
      
        this.getFullName = function() {
          return getFirstName() + ' ' + getLastName();
        }
      
      }
      
      var user = new User('Ahmad', 'Fares');
      
      console.log(user.getFullName());
      1
    • Sử dụng
      function User(firstName, lastName) {
      
        var self = this;
      
        this.firstName = firstName;
        this.lastName = lastName;
      
        function getFirstName() {
          return self.firstName;
        }
      
        function getLastName() {
          return self.lastName;
        }
      
        this.getFullName = function() {
          return getFirstName() + ' ' + getLastName();
        }
      
      }
      
      var user = new User('Ahmad', 'Fares');
      
      console.log(user.getFullName());
      2, tốt hơn vì nếu chúng ta có các thuộc tính lưu trữ
      function User(firstName, lastName) {
      
        var self = this;
      
        this.firstName = firstName;
        this.lastName = lastName;
      
        function getFirstName() {
          return self.firstName;
        }
      
        function getLastName() {
          return self.lastName;
        }
      
        this.getFullName = function() {
          return getFirstName() + ' ' + getLastName();
        }
      
      }
      
      var user = new User('Ahmad', 'Fares');
      
      console.log(user.getFullName());
      3
  • Toán tử đẳng thức
    function User(firstName, lastName) {
    
      var self = this;
    
      this.firstName = firstName;
      this.lastName = lastName;
    
      function getFirstName() {
        return self.firstName;
      }
    
      function getLastName() {
        return self.lastName;
      }
    
      this.getFullName = function() {
        return getFirstName() + ' ' + getLastName();
      }
    
    }
    
    var user = new User('Ahmad', 'Fares');
    
    console.log(user.getFullName());
    4 và đẳng thức nghiêm ngặt
    function User(firstName, lastName) {
    
      var self = this;
    
      this.firstName = firstName;
      this.lastName = lastName;
    
      function getFirstName() {
        return self.firstName;
      }
    
      function getLastName() {
        return self.lastName;
      }
    
      this.getFullName = function() {
        return getFirstName() + ' ' + getLastName();
      }
    
    }
    
    var user = new User('Ahmad', 'Fares');
    
    console.log(user.getFullName());
    5 cho các đối tượng hoạt động giống hệt nhau
  • Nhân bản và hợp nhất
    • Sao chép nông và hợp nhất
      • function User(firstName, lastName) {
        
          var self = this;
        
          this.firstName = firstName;
          this.lastName = lastName;
        
          function getFirstName() {
            return self.firstName;
          }
        
          function getLastName() {
            return self.lastName;
          }
        
          this.getFullName = function() {
            return getFirstName() + ' ' + getLastName();
          }
        
        }
        
        var user = new User('Ahmad', 'Fares');
        
        console.log(user.getFullName());
        6
      • function User(firstName, lastName) {
        
          var self = this;
        
          this.firstName = firstName;
          this.lastName = lastName;
        
          function getFirstName() {
            return self.firstName;
          }
        
          function getLastName() {
            return self.lastName;
          }
        
          this.getFullName = function() {
            return getFirstName() + ' ' + getLastName();
          }
        
        }
        
        var user = new User('Ahmad', 'Fares');
        
        console.log(user.getFullName());
        7
      • function User(firstName, lastName) {
        
          var self = this;
        
          this.firstName = firstName;
          this.lastName = lastName;
        
          function getFirstName() {
            return self.firstName;
          }
        
          function getLastName() {
            return self.lastName;
          }
        
          this.getFullName = function() {
            return getFirstName() + ' ' + getLastName();
          }
        
        }
        
        var user = new User('Ahmad', 'Fares');
        
        console.log(user.getFullName());
        8 không được hỗ trợ trong
        function User(firstName, lastName) {
        
          var self = this;
        
          this.firstName = firstName;
          this.lastName = lastName;
        
          function getFirstName() {
            return self.firstName;
          }
        
          function getLastName() {
            return self.lastName;
          }
        
          this.getFullName = function() {
            return getFirstName() + ' ' + getLastName();
          }
        
        }
        
        var user = new User('Ahmad', 'Fares');
        
        console.log(user.getFullName());
        9, chúng tôi có thể sử dụng
        if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        00
      • if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        01
    • Bản sao sâu
      • if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        02 từ Lodash
  • Có các loại đối tượng khác ngoài
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    03 (hay còn gọi là.
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    04)
    • Mảng
    • Ngày
    • Lỗi
  • Để kiểm tra xem đối tượng có nghĩa đen không.
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    05
  • Đối tượng toàn cầu trong trình duyệt là
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    06
  • Đối tượng toàn cục trong Node. js là
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    07
OOP

Hàm xây dựng

  • Là các hàm thông thường
  • Theo quy ước, nó phải được đặt tên bằng chữ in hoa trước và không được gọi nếu không có
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    08
  • Khi chức năng được thực thi như
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    09, nó sẽ
    • Tạo một đối tượng trống
    • Gán
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      10 của đối tượng đã tạo cho
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      11
    • Thực thi chức năng và gán
      Array.prototype.slice.call(arguments);
      5 cho đối tượng đã tạo
    • Trả về giá trị của
      Array.prototype.slice.call(arguments);
      5 hoặc giá trị của câu lệnh
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      14 nếu kết quả trả về là một đối tượng
  • Giá trị mặc định
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    15 là một đối tượng có thuộc tính duy nhất
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    16 trỏ về chính hàm đó
  • Nếu chúng ta thay thế toàn bộ nguyên mẫu mặc định, thì sẽ không có
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    16 trong đó, trừ khi chúng ta xác định thủ công
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    18 rất hữu ích khi chúng ta muốn tạo một đối tượng khác từ cùng một hàm tạo mà chúng ta không biết.
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    19
  • Có thể chạy bất kỳ chức năng nào với
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    08
  • Để kiểm tra xem hàm có được gọi với
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    08 hay không, chúng ta có thể sử dụng
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    22 bên trong nó

Để cho phép tạo một đối tượng có hoặc không có

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
08 (không khuyến nghị)

function User(name) {

  // Not called with new
  if (!new.target) {
    return new User(name);
  }

  this.name = name;
  
}

Mẫu

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
24 để đóng gói việc tạo một đối tượng

var user = new function() {
  this.name = 'Anas';
  this.age = 'Ali';
}

nguyên mẫu

  • Tất cả các đối tượng đều có một thuộc tính
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    25 ẩn đó là một đối tượng khác hoặc
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    1
  • Đối tượng nguyên mẫu đó có một nguyên mẫu của riêng nó, v.v. cho đến khi đạt được một đối tượng có
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    1 làm nguyên mẫu
  • Chỉ có thể là một
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    25 không có nhiều thừa kế
  • Nguyên mẫu chỉ được sử dụng để đọc thuộc tính
  • Thao tác ghi/xóa làm việc trực tiếp với đối tượng
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    29 sẽ không có hiệu lực nếu tài sản được thừa kế (chuỗi nguyên mẫu)
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    30 sẽ tạo một thuộc tính trên đối tượng này bất kể có thuộc tính tên là
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    31 được kế thừa hay không
  • Array.prototype.slice.call(arguments);
    5 hoàn toàn không bị ảnh hưởng bởi nguyên mẫu
  • Bất kể phương pháp được tìm thấy ở đâu. trong một đối tượng hoặc nguyên mẫu của nó. Trong một cuộc gọi phương thức,
    Array.prototype.slice.call(arguments);
    5 luôn là đối tượng trước dấu chấm
  • function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    1 không có nguyên mẫu và đóng vai trò là liên kết cuối cùng trong chuỗi nguyên mẫu này
  • Gần như tất cả các đối tượng trong JavaScript là phiên bản của
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    03
  • Để truy cập vào
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    25
    • Sử dụng thuộc tính phi tiêu chuẩn
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      37
    • Sử dụng ES2015
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      38,
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      39
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    37 là setter/getter cho
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    25, được định nghĩa trong
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    42
  • Nếu một đối tượng mà chuỗi nguyên mẫu của nó không kết thúc bằng
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    03, thì
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    37 sẽ không khả dụng
  • Toán tử
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    45 kiểm tra chuỗi nguyên mẫu để kiểm tra
    • if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      46 là
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      47 vì
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      48

Mục tiêu. tạo nên()

Tạo một đối tượng trống với proto đã cho là

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
25 (có thể là null) và các bộ mô tả thuộc tính tùy chọn

Object.create(proto[, descriptors])

Chúng ta có thể sử dụng

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
50 để thực hiện nhân bản đối tượng

var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));

Để tạo một đối tượng trống không có nguyên mẫu

Object.create(null)

Nguyên mẫu bản địa

  • JavaScript dựa trên nguyên mẫu
  • Không có thêm
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    25 trong chuỗi trên
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    42
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    53 là
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    1
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    55
  • if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    56
  • Việc sửa đổi các nguyên mẫu gốc không được khuyến nghị trừ khi đối với các polyfill

mẫu lớp

  • Mô hình lớp chức năng
    • Tạo một đối tượng bằng cách sử dụng
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      08 và hàm tạo có mọi thứ bên trong nó
    • Các thuộc tính công khai được định nghĩa bên trong hàm tạo
    • Các phương thức công khai được định nghĩa bên trong hàm tạo
    • Có thể có _______46_______58 phương thức/thuộc tính không được truy cập từ bên ngoài
  • Mẫu lớp nhà máy
    • Tạo một đối tượng bằng cách gọi một hàm trả về một đối tượng mới
    • Thuộc tính công cộng được xác định bên trong đối tượng được trả về
    • Các phương thức công khai được định nghĩa bên trong đối tượng được trả về
    • Có thể có _______46_______58 phương thức không được truy cập từ bên ngoài
  • Mẫu lớp dựa trên nguyên mẫu
    • Tạo một đối tượng bằng cách sử dụng
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      08 và hàm tạo
    • Các thuộc tính công khai được định nghĩa bên trong hàm tạo
    • Các phương thức công khai được định nghĩa bên trong thuộc tính nguyên mẫu của hàm tạo
    • Không thể có các phương thức
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      58, thêm tiền tố bằng
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      62 là một quy ước để chỉ ra rằng một thành viên không nên được sử dụng từ bên ngoài
    • Hàm tạo chỉ khởi tạo trạng thái đối tượng hiện tại
    • Tiết kiệm bộ nhớ hơn vì tất cả các phương thức được chia sẻ giữa tất cả các đối tượng
    • Là tốt nhất và chủ yếu được sử dụng

Ghi chú

  • Đối với

    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    63, các phương thức
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    58 được xác định đơn giản trong phạm vi của hàm tạo, vì vậy bất kỳ phương thức thành viên công khai nào cũng sẽ có quyền truy cập vào phạm vi bên ngoài để chúng ta có thể gọi chúng từ bất kỳ hàm thành viên nào, nhưng chúng sẽ được thực thi trong ngữ cảnh khác (không'

    • Khi tạo các phương thức riêng tư, hãy liên kết chúng với bối cảnh hiện tại bằng cách sử dụng
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      66
    • Gọi các phương thức riêng tư bằng cách sử dụng
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      67 hoặc
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      68 với
      Array.prototype.slice.call(arguments);
      5 làm ngữ cảnh
    • Xác định một biến tham chiếu
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      70 cho đối tượng hiện tại bên trong hàm tạo và sử dụng nó bên trong các phương thức riêng tư thay vì
      Array.prototype.slice.call(arguments);
      5
  • Đối với

    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    72, chúng ta có thể xác định một tham chiếu đến đối tượng được trả về bên trong hàm xuất xưởng và sử dụng nó thay vì
    Array.prototype.slice.call(arguments);
    5 với các tùy chọn trên

Ví dụ mẫu chức năng

function User(firstName, lastName) {

  var self = this;

  this.firstName = firstName;
  this.lastName = lastName;

  function getFirstName() {
    return self.firstName;
  }

  function getLastName() {
    return self.lastName;
  }

  this.getFullName = function() {
    return getFirstName() + ' ' + getLastName();
  }

}

var user = new User('Ahmad', 'Fares');

console.log(user.getFullName());

Ví dụ mẫu giai thừa

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
0

Ví dụ về mẫu dựa trên nguyên mẫu

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
1

Di sản

  • mô hình cổ điển
    • Các lớp được đại diện bởi các hàm xây dựng
    • Để kế thừa các phương thức
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      74
    • Đặt lại thuộc tính hàm tạo
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      75
    • Để kế thừa các thuộc tính
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      76 bên trong hàm tạo
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      77
  • mô hình nguyên mẫu
    • Các lớp chỉ là đối tượng theo nghĩa đen
    • Để thừa kế
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      78
Chức năng

4 cách để tạo một chức năng

  • khai báo hàm
    • Được nâng lên, Được xử lý trước khi khối mã được thực thi. Chúng có thể nhìn thấy ở mọi nơi trong khối
    • Tuyên bố góp phần vào
      Array.prototype.slice.call(arguments);
      8 khi phạm vi thực hiện được nhập
  • biểu thức hàm
    • Được tạo khi luồng thực thi đến chúng
    • Nếu họ được gán tên thì tên đó chỉ hiển thị một mình
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      80
  • Hàm tạo hàm
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    81
  • chức năng mũi tên
    • Không có
      Array.prototype.slice.call(arguments);
      5
    • Không có
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      83
    • Không thể được gọi với
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      08
    • Không có
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      85 của riêng họ, hoạt động trong cái hiện tại

ghi chú

  • Thuộc tính của hàm
    • Tên. tên của chức năng
    • chiều dài. số đối số
    • nguyên mẫu
    • nguyên mẫu. hàm tạo
  • __proto__. Hàm số. nguyên mẫu
Lập kế hoạch với bộ hẹn giờ
  • Sử dụng các hàm
    Object.create(null)
    0 và
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    87
  • Object.create(null)
    0 có thể được sử dụng để thực hiện
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    87
  • Chúng không phải là một phần của đặc tả JavaScript (ES)
  • Lập lịch thời gian chờ bằng không
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    90
    • Được sử dụng để lên lịch cuộc gọi ngay khi có thể, nhưng sau khi mã hiện tại hoàn tất
    • Để chia các tác vụ ngốn CPU thành nhiều phần, để tập lệnh không bị treo
    • Hãy để trình duyệt làm việc khác trong khi quá trình đang diễn ra
Loại dữ liệu
  • gõ động
  • Có các kiểu dữ liệu, nhưng các biến không bị ràng buộc với bất kỳ kiểu dữ liệu nào
  • Không có loại
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    91, chúng thuộc loại đối tượng, nhưng
    function User(firstName, lastName) {
    
      var self = this;
    
      this.firstName = firstName;
      this.lastName = lastName;
    
      function getFirstName() {
        return self.firstName;
      }
    
      function getLastName() {
        return self.lastName;
      }
    
      this.getFullName = function() {
        return getFirstName() + ' ' + getLastName();
      }
    
    }
    
    var user = new User('Ahmad', 'Fares');
    
    console.log(user.getFullName());
    0 đối xử với chúng theo cách khác
  • Một lỗi cũ trong JavaScript là
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    93
  • Bảy kiểu dữ liệu cơ bản
    • số
      • Phục vụ cho cả số nguyên và số dấu phẩy động
      • Có các giá trị số đặc biệt.
        if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        94,
        if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        95 và
        if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        96
      • Toán học an toàn trong JavaScript, sẽ không bao giờ dừng lại với một lỗi nghiêm trọng
      • if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        96 đại diện cho một lỗi tính toán
      • if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        98;
      • if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        99;
      • function wrapper() {
          return printArgs.apply(this, arguments);
        }
        00 được đánh giá thành
        if (!('bind' in Function.prototype)) {
          Function.prototype.bind = function() {
            var func = this;
            var obj = arguments[0];
            var params = Array.prototype.slice.call(arguments, 1);
            return function() {
              func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
            }
          }
        }
        96
    • chuỗi
      • Được mã hóa bằng UTF-16
      • Bất biến, không thể thay đổi ký tự
    • boolean
    • vô giá trị
      • Loại độc lập có một giá trị duy nhất
        function User(name) {
        
          // Not called with new
          if (!new.target) {
            return new User(name);
          }
        
          this.name = name;
          
        }
        1
      • Để chỉ ra
        function wrapper() {
          return printArgs.apply(this, arguments);
        }
        03 hoặc
        function wrapper() {
          return printArgs.apply(this, arguments);
        }
        04
    • chưa xác định
      • Loại độc lập có một giá trị duy nhất
        function User(firstName, lastName) {
        
          var self = this;
        
          this.firstName = firstName;
          this.lastName = lastName;
        
          function getFirstName() {
            return self.firstName;
          }
        
          function getLastName() {
            return self.lastName;
          }
        
          this.getFullName = function() {
            return getFirstName() + ' ' + getLastName();
          }
        
        }
        
        var user = new User('Ahmad', 'Fares');
        
        console.log(user.getFullName());
        3
      • Để chỉ ra
        function wrapper() {
          return printArgs.apply(this, arguments);
        }
        06
      • Chỉ được sử dụng để kiểm tra
    • mục tiêu
      • Không phải là một loại nguyên thủy
      • Thu thập dữ liệu
    • Biểu tượng
      • Đối với số nhận dạng duy nhất

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
2

Chuyển đổi loại

chuyển đổi ngầm định

  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    07 tự động chuyển đổi bất kỳ giá trị nào thành chuỗi
  • Các phép toán chuyển đổi giá trị thành số
  • Nếu biểu thức điều kiện được chuyển đổi thành boolean

chuyển đổi rõ ràng

thành chuỗi

  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    08 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    09
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    10 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    11
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    12 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    13
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    14 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    13
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    16 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    17

Đến số

  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    18 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    19
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    20 trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    96
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    22 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    19
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    24 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    25
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    26 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    27
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    28 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    27
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    30 trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    96
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    32 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    19
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    34 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    19

ghi chú

  • Để chuyển đổi một chuỗi thành một số
    • Cắt khoảng trắng (dấu cách, tab, dòng mới)
    • Một chuỗi rỗng trở thành
      function wrapper() {
        return printArgs.apply(this, arguments);
      }
      19
    • Một lỗi đưa ra
      if (!('bind' in Function.prototype)) {
        Function.prototype.bind = function() {
          var func = this;
          var obj = arguments[0];
          var params = Array.prototype.slice.call(arguments, 1);
          return function() {
            func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
          }
        }
      }
      96
  • Thao tác nối so với dấu cộng
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      38
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      39
  • Cũng có thể chuyển đổi thành số với
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    40 và
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    41
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      42
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      43

để Boolean

Sai. các giá trị trống bằng trực giác

  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    44 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    45
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    46 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    45
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    48 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    45
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    50 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    45
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    52 trở thành
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    45

Thật. nếu không thì

  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    54 trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    47
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    56 trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    47
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    58 trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    47
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    60 trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    47

ghi chú

  • xảy ra trong các hoạt động logic
  • Phương pháp nguyên thủy
    • Nguyên thủy ngoại trừ
      function User(name) {
      
        // Not called with new
        if (!new.target) {
          return new User(name);
        }
      
        this.name = name;
        
      }
      1 và
      function User(firstName, lastName) {
      
        var self = this;
      
        this.firstName = firstName;
        this.lastName = lastName;
      
        function getFirstName() {
          return self.firstName;
        }
      
        function getLastName() {
          return self.lastName;
        }
      
        this.getFullName = function() {
          return getFirstName() + ' ' + getLastName();
        }
      
      }
      
      var user = new User('Ahmad', 'Fares');
      
      console.log(user.getFullName());
      3 cung cấp nhiều phương pháp hữu ích
    • Các phương thức này hoạt động thông qua các đối tượng trình bao bọc tạm thời, nhưng các công cụ JavaScript được điều chỉnh tốt để tối ưu hóa nội bộ đó
    • Như
      function wrapper() {
        return printArgs.apply(this, arguments);
      }
      64,
      function wrapper() {
        return printArgs.apply(this, arguments);
      }
      65
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    66 trả về một
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    67 không nguyên thủy
Đối tượng chuyển đổi nguyên thủy
  • Tất cả các đối tượng sẽ trở thành
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    47 nếu được chuyển đổi thành boolean
  • Để chuyển đổi sang chuỗi sẽ sử dụng các phương thức
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    69 hoặc
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    70
  • Để chuyển đổi sang số, các phương pháp
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    71 sẽ được sử dụng hoặc
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    72
nhà điều hành
  • Toán tử dấu phẩy
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    73
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      74
    • Đánh giá cả hai, trả về cái cuối cùng
so sánh
  • Các toán tử so sánh trả về một giá trị logic
  • Các chuỗi được so sánh từng chữ cái theo thứ tự "từ điển"
  • Khi các giá trị của các loại khác nhau được so sánh, chúng sẽ được chuyển đổi thành số (không bao gồm kiểm tra đẳng thức nghiêm ngặt)
  • Các giá trị
    function User(name) {
    
      // Not called with new
      if (!new.target) {
        return new User(name);
      }
    
      this.name = name;
      
    }
    1 và
    function User(firstName, lastName) {
    
      var self = this;
    
      this.firstName = firstName;
      this.lastName = lastName;
    
      function getFirstName() {
        return self.firstName;
      }
    
      function getLastName() {
        return self.lastName;
      }
    
      this.getFullName = function() {
        return getFirstName() + ' ' + getLastName();
      }
    
    }
    
    var user = new User('Ahmad', 'Fares');
    
    console.log(user.getFullName());
    3 bằng nhau ____67_______4 và không bằng bất kỳ giá trị nào khác
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      78 là đúng
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      79 là đúng
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      80 là đúng
  • Một không thể so sánh được không xác định, giống như
    if (!('bind' in Function.prototype)) {
      Function.prototype.bind = function() {
        var func = this;
        var obj = arguments[0];
        var params = Array.prototype.slice.call(arguments, 1);
        return function() {
          func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
        }
      }
    }
    96 (luôn luôn sai)
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      82 là sai
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      83 là sai
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      84 là sai
    • function wrapper() {
        return printArgs.apply(this, arguments);
      }
      85 là sai
Toán tử logic
  • Hoặc (
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    86)
    • OR trả về giá trị trung thực đầu tiên hoặc giá trị cuối cùng nếu không tìm thấy giá trị như vậy
  • Và (
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    87)
    • AND trả về giá trị giả đầu tiên hoặc giá trị cuối cùng nếu không tìm thấy
  • Ưu tiên của
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    87 cao hơn
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    86

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
3

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
4

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
5

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
6

Ví dụ về chức năng mũi tên

Nhiều đối số

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
7

đối số duy nhất

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
8

Hàm mũi tên nhiều dòng

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
9

Giá trị mặc định

  • Nếu một tham số không được cung cấp, thì giá trị của nó sẽ không được xác định
  • Giá trị mặc định cho tham số (không được hỗ trợ trong IE)
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    90
  • Đang kiểm tra
    function User(firstName, lastName) {
    
      var self = this;
    
      this.firstName = firstName;
      this.lastName = lastName;
    
      function getFirstName() {
        return self.firstName;
      }
    
      function getLastName() {
        return self.lastName;
      }
    
      this.getFullName = function() {
        return getFirstName() + ' ' + getLastName();
      }
    
    }
    
    var user = new User('Ahmad', 'Fares');
    
    console.log(user.getFullName());
    3 của
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    92
  • Bằng cách sử dụng toán tử
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    86,
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    94

ghi chú

  • Một biến được khai báo bên trong một hàm chỉ hiển thị bên trong hàm đó
  • Hàm là một đối tượng mà nguyên mẫu của nó trỏ tới
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    95

function wrapper() {
  return printArgs.apply(this, arguments);
}
0

Sử dụng nghiêm ngặt
  • Áp dụng cho toàn bộ tập lệnh hoặc cho các chức năng riêng lẻ
  • Chế độ nghiêm ngặt thay đổi ngữ nghĩa. Dựa vào những thay đổi đó sẽ gây ra lỗi và lỗi trong các trình duyệt không thực hiện chế độ nghiêm ngặt
  • function wrapper() {
      return printArgs.apply(this, arguments);
    }
    96 hoặc
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    97 có thể được đặt ở đầu tập lệnh hoặc ở đầu hàm
  • Chỉ nhận xét có thể xuất hiện trên
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    97
  • Không có cách nào để hủy bỏ
    function wrapper() {
      return printArgs.apply(this, arguments);
    }
    99
  • nó được khuyến khích

Một số khác biệt

Không thể vô tình tạo các biến toàn cục

function wrapper() {
  return printArgs.apply(this, arguments);
}
1

Ném lỗi khi cố xóa thuộc tính không thể xóa

function wrapper() {
  return printArgs.apply(this, arguments);
}
2

Ném lỗi khi cố gắng thay đổi thuộc tính không thể ghi

function wrapper() {
  return printArgs.apply(this, arguments);
}
3

Ném lỗi khi tên đối số hàm không phải là duy nhất

function wrapper() {
  return printArgs.apply(this, arguments);
}
4

Không còn có thể tham chiếu đối tượng

if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
07 đến
Array.prototype.slice.call(arguments);
5 bên trong một hàm

function wrapper() {
  return printArgs.apply(this, arguments);
}
5

Các thuộc tính

Array.prototype.slice.call(arguments);
02,
Array.prototype.slice.call(arguments);
03 và
if (!('bind' in Function.prototype)) {
  Function.prototype.bind = function() {
    var func = this;
    var obj = arguments[0];
    var params = Array.prototype.slice.call(arguments, 1);
    return function() {
      func.apply(obj, params.concat(Array.prototype.slice.call(arguments)));
    }
  }
}
83 có thể không được truy cập trên các chức năng chế độ nghiêm ngặt