Ở bài viết trước - Các hàm JavaScript thiết yếu để tăng lực cho ứng dụng web - jsB@nk đã giới thiệu với bạn một hàm JavaScript có thể sao chép một đối tượng JavaScript, nhưng lại không có nhiều hướng dẫn cũng như chú thích.
Vì vậy trong bài viết này, jsB@nk sẽ cung cấp cho bạn một cái nhìn chi tiết hơn về vấn đề sao chép một đối tượng JavaScript cùng với rất nhiều các trường hợp liên quan phát sinh thêm. Bạn vui lòng vào trang chi tiết để xem thêm.
- Demo
- Phóng to
- Tải lại
- Cửa sổ mới
Miễn phí web hosting 1 năm đầu tại iPage
Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?
Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.
Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Everyone knows Javascript doesnt have classes. Thankfully most Javascript programmers are very good at playing pretend.
Because so many people attempt to introduce a classical structure to their Javscript code, I figure Ill share some of my findings. Below is one way of allowing a base class function to copy an instance of a subclass while maintaining proper type.
Lets say we have an Animal class and we subclass it to create a Cat class. We also want the Animal class to have a clone() method that will return work with its subclasses. That is, if I clone a Cat, I want instanceof Cat and instanceof Animal to both be true.
Lets make the classes real quick. Heres an Animal:
function Animal() { // animal stuff } // All of Animal's methods will go into its prototype Animal.prototype = { saySomething: function() { return "I am an animal!"; }, copy: function() { var newcopy; if (this.subConstructor) { newcopy = new this.subConstructor; } else { newcopy = new Animal(); } // maybe copy over other values return newcopy; } };
And then the Cat. Typically the easiest way to make a Javascript function be a subclass of another function is to set the prototype of the subclass equal to new baseclass. With cat well go a little further and also save the constructor.
Cat:
function Cat() { this.constructor(); // call animal's constructor // cat stuff } // Save the cat's constructor, replace Cat's prototype and constructor // Then restore cat's constructor as subConstructor var con = Cat.prototype.constructor; Cat.prototype = new Animal(); Cat.prototype.constructor = Animal; Cat.prototype.subConstructor = con // override the saySomething function Cat.prototype.saySomething = function() { return "Meow!"; };
There you have it. Cats created this way will be instanceof Cat and instanceof Animal, and so will their clones. We should of course make tests to see, and lucky for us all of them check out OK:
// some tests and their results in the comment var c = new Cat(); console.log(c instanceof Animal); // true console.log(c instanceof Cat); // true console.log(c.saySomething()); // "Meow!" var a = new Animal(); console.log(a instanceof Animal); // true console.log(a instanceof Cat); // false console.log(a.saySomething()); // "I am an animal!" var copyCat = c.copy(); console.log(copyCat instanceof Animal); // true console.log(copyCat instanceof Cat); // true console.log(copyCat.saySomething()); // "Meow!" var copyAnimal = a.copy(); console.log(copyAnimal instanceof Animal); // true console.log(copyAnimal instanceof Cat); // false console.log(copyAnimal.saySomething()); // "I am an animal!"
You might want to use the four lines needed for subclassing elsewhere so its a good idea to stuff them into a function that youd put in a utility class/var somewhere. So we could make:
function derive(base, sub) { var con = sub.prototype.constructor; sub.prototype = new base(); sub.prototype.constructor = base; sub.prototype.subConstructor = con }
And then making a clone-able subclass would be as easy as calling derive after each constructor:
function Cat() { this.constructor(); // call animal's constructor // cat stuff } derive(Animal, Cat); // override the saySomething function Cat.prototype.saySomething = function() { return "Meow!"; };
- Lượt gửi (0)
- Mới
Save up to 630$ when buy new iPhone 15
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language
Dall-E-OpenAI.com
Generate creative images automatically with AI
AIVideo-App.com
Render creative video automatically with AI
Phản hồi