Đang chuẩn bị liên kết để tải về tài liệu:
Học JavaScript qua ví dụ part 24

Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG

Thêm biện pháp khác với nguyên mẫu JavaScript phương pháp chỉ là tài sản đã được phân công chức năng cho các giá trị của họ. Cách sử dụng chung cho các nguyên mẫu là để mở rộng một lớp học bằng cách cho nó phương pháp mới. | 202 Chapter 8 Objects Figure 8.16 Going up the prototype chain. 8.5.3 Adding Methods with Prototype JavaScript methods are just properties that have been assigned functions for their values. A common use for prototypes is to extend a class by giving it new methods. You can use a prototype to assign methods that are common to all objects in a given class. The benefit of using the prototype is that the method does not have to be created and initialized every time an object is created and there is no duplication of function code. All objects in the class share the same prototype method. In Example 8.14 a new method is defined for the Book class to add a tax to the price of each book. All Book objects will have access to this method. EXAMPLE 8.14 html head title Method Inheritance title script type text javascript 1 function Book title author price The Book constructor this.title title Book properties attributes this.author author this.price price 2 this.show showProps Book method 3 function showProps var result for var i in this result i this i br return result script head body script type text javascript Add a new method with prototype From the Library of WoweBook.Com 8.5 Extending Objects with Prototypes 203 EXAMPLE 8.14 4 Book.prototype.addTax function addTax this.price 1.18 return this.price.toFixed 2 5 var famousBook new Book War and Peace Leo Tolstoy 30.00 var myBook new Book JavaScript by Example Ellie Quigley 25 6 document.write br b famousBook.show br document.write br b myBook.show br 7 document.write With tax famousBook.title costs famousBook.addTax . br document.write With tax myBook.title costs 8 myBook.addTax . br script body html EXPLANATION 1 The constructor function called Book defines the properties and methods for a Book class. Book is a JavaScript representation of a class. The constructor function has a prototype object containing those properties that are inherited by all Book objects. 2 A function called showProps is defined on line 3. Its name

Đã 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.