Data SharingWhat kind of data needs to be written in the prototype?The data that needs to be shared can be written in the prototype One of the functions of prototype: data sharing Attributes need to be shared, and methods also need to be shared:
Let’s look at a case Data Sharing CasesEach student's name, age, and gender are unique. We need to set
You can write the common data into the prototype <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> function Student(name,age,sex) { this.name=name; this.age=age; this.sex=sex; } // All students' height is 188, and all students' weight is 55 // All students must write 500 lines of code every day // All students must eat a 10-pound watermelon every day // Prototype object Student.prototype.height="188"; Student.prototype.weight="55kg"; Student.prototype.study = function () { console.log("Learning, writing 500 lines of code, a piece of cake"); }; Student.prototype.eat = function () { console.log("Eat a 10-pound watermelon"); }; //Instantiate the object and initialize it var stu = new Student ("晨光", 57,"女"); console.dir(Student); console.dir(stu); // stu.eat(); // stu.study(); </script> </head> <body> </body> </html> This is what it prints out: Simple prototype writingThere is also a simpler method for prototypes. The following is a modification of the above example <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> function Student(name, age, sex) { this.name = name; this.age = age; this.sex = sex; } //Simple prototype writing Student.prototype = { //Manually modify the constructor to point to constructor:Student, height: "188", weight: "55kg", study: function () { console.log("It's so fun to study"); }, eat: function () { console.log("I want to eat delicious food"); } }; var stu=new Student("Ding Ding Da Ming",20,"Male"); stu.eat(); stu.study(); console.dir(Student); console.dir(stu); </script> </head> <body> </body> </html> Prototype method sharingFor example, set the method to play after eating and sleep after playing. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> //Methods in the prototype can access each other function Animal(name,age) { this.name=name; this.age=age; } //Add method in prototype// Play after eating Animal.prototype.eat=function () { console.log("animals eat"); this.play(); }; // Sleep after playing Animal.prototype.play=function () { console.log("playing ball"); this.sleep(); }; Animal.prototype.sleep = function () { console.log("sleep"); }; var dog = new Animal ("Xiao Su", 20); dog.eat(); //Methods in the prototype object can call each other</script> </head> <body> </body> </html> This is the end of this article about exploring the implementation of JavaScript prototype data sharing and method sharing. For more relevant content about exploring JavaScript prototype data sharing and method sharing, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of redis5 cluster construction and usage under Linux (Centos7)
>>: mysql zip file installation tutorial
Clickhouse Introduction ClickHouse is a column-or...
Recently, I need to use a lot of fragmented pictu...
Table of contents Mistake 1: Too many columns of ...
There is such a requirement: an import button, cl...
This article example shares the specific code of ...
MySQL group sorting to find the top N Table Struc...
Starting and shutting down Tomcat under Linux In ...
1. Set CORS response header to achieve cross-doma...
I wonder if you are like me, a programmer who arr...
1. SSH remote management SSH is a secure channel ...
Table of contents Master-slave replication mechan...
mistake The following error occurs when connectin...
Table of contents Enter the topic mysql add, dele...
1. Install tools and libraries # PCRE is a Perl l...
Visual Studio Code is a powerful text editor prod...