TAILIEUCHUNG - Học JavaScript qua ví dụ part 21

Các lớp và hàm User-Defined Một lớp là gì? JavaScript là không có không có lớp học! Nếu bạn đã quen thuộc với Java hay C + + bạn có thể tự hỏi làm thế nào để tạo ra một lớp trong JavaScript. Lớp là một mẫu hoặc kế hoạch chi tiết mô tả các thuộc tính và hành vi của tất cả các đối tượng thuộc tầng lớp cụ thể, vì vậy bạn có thể có một loại xe hơi hoặc lớp một nhà hoặc một lớp Widget | 182 Chapter 8 Objects Figure An object and its properties. Classes and User-Defined Functions What Is a Class JavaScript ain t got no class If you are familiar with Java or C you may be wondering how to create a class in JavaScript. A class is a template or blueprint that describes the properties and behavior of all objects that belong to that specific class so you may have a Car class or a House class or a Widget class. A Car class would be defined with the properties and methods for a Car and then you could create as many Car objects as you want using the class as a template. But JavaScript doesn t have classes in the traditional sense. It doesn t have a class keyword. We must develop the notion of classes in a different way. A new JavaScript class is defined by creating a simple function. The name of the function will serve as the class name for an object and the function will define its properties and methods it serves as a blueprint or prototype of the object. When the function is called with the new keyword it acts as a constructor that is it builds the new object and then returns a reference to it. We can say that if you call the Book constructor function it returns a reference to a new Book object an instance of the Book class. What Is this Internally JavaScript creates an object and then calls the constructor function. Inside the constructor the variable this is initialized to point to this newly created object. The this keyword is a sort of shorthand reference that keeps track of the current object. When a function is used as a constructor the this keyword is used to set the properties for the object that was just created. In this way you can create as many objects as you need and JavaScript this will refer to the current object. In Example a function is used to create a Book class with two properties a title and an author. The this keyword refers to the current Book object. Methods can be assigned to an object in the constructor .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.