Introduction: Most articles about Imagine we are making a strategy war game where players can control a bunch of soldiers to attack the enemy. Let’s focus on the “making soldiers” part of this game. A soldier is represented by a bunch of attributes in the computer, as shown below: 1. ExampleWe can create a soldier just like this: var Soldier = { ID: 1, // Used to distinguish each soldier's type: "American Soldier", Attack power: 5, HP:42, Walking: function(){ /*code for walking two steps*/}, Run: function(){ /*Running code*/ }, Death: function(){ /*Go die*/ }, Attack: function(){ /*Brush his bear face*/ }, Defense: function(){ /*Protect the face*/ } } Barracks. Manufacturing (soldiers) 2. Create 100 soldiersWhat if you need to create 100 soldiers? Let’s loop 100 times: var soldiers = [] var Soldier for(var i=0; i<100; i++){ Soldier = { ID: i, // ID cannot be repeated. Soldier: "American Soldier", Attack power: 5, HP:42, Walking: function(){ /*code for walking two steps*/}, Run: function(){ /*Running code*/ }, Death: function(){ /*Go die*/ }, Attack: function(){ /*Brush his bear face*/ }, Defense: function(){ /*Protect the face*/ } } Soldiers.push(soldier) } Barracks. Mass production (soldiers) 3. QuestioningThere is a problem with the code above: a lot of memory is wasted.
4. Improvements Those who have read our previous column articles (JS prototype chain) must know that the prototype chain can solve the problem of repeated creation: we first create a "soldier prototype", and then let var SoldierPrototype = { Arms: "American Soldier", Attack power: 5, Walking: function(){ /*code for walking two steps*/}, Run: function(){ /*Running code*/ }, Death: function(){ /*Go die*/ }, Attack: function(){ /*Brush his bear face*/ }, Defense: function(){ /*Protect the face*/ } } var soldiers = [] var Soldier for(var i=0; i<100; i++){ Soldier = { ID: i, // ID cannot be repeated Life value: 42 } /*Don't write this in actual work, because __proto__ is not a standard attribute*/ Soldier.__proto__ = Soldier prototype Soldiers.push(Soldier) } Barracks. Mass production (soldiers) 5. Elegant?Someone pointed out that it's not elegant to have the code for creating a soldier in two places, so we use a function to connect the two parts: function soldier(ID){ var temporary object = {} temporary object.__proto__ = soldier.prototype temporary object.ID = ID TemporaryObject.LifeValue = 42 return temporary object} Soldier.Prototype = { Arms: "American Soldier", Attack power: 5, Walking: function(){ /*code for walking two steps*/}, Run: function(){ /*Running code*/ }, Death: function(){ /*Go die*/ }, Attack: function(){ /*Brush his bear face*/ }, Defense: function(){ /*Protect the face*/ } } // Save as file: soldier.js Then you can happily reference "Soldier" to create a soldier: var soldiers = [] for(var i=0; i<100; i++){ soldiers.push(soldier(i)) } Barracks. Mass production (soldiers) 6. Care from the Father of JS The creator of JS created the As long as you use the
7. This time we use new to writefunction soldier(ID){ this.ID = ID this.health = 42 } Soldier.prototype = { Arms: "American Soldier", Attack power: 5, Walking: function(){ /*code for walking two steps*/}, Run: function(){ /*Running code*/ }, Death: function(){ /*Go die*/ }, Attack: function(){ /*Brush his bear face*/ }, Defense: function(){ /*Protect the face*/ } } // Save as file: soldier.js Then create the soldier (add a new keyword): var soldiers = [] for(var i=0; i<100; i++){ Soldiers.push(new Soldier(i)) } Barracks. Mass production (soldiers) The purpose of new is to save a few lines of code. (Also known as syntactic sugar) 8. Pay attention to the constructor attribute In order to record "which function created the temporary object", the Soldier.prototype = { constructor: Soldier} If you reassign " Soldier.prototype.ArmyType = "American Soldier" Soldier.prototype.attack = 5 Soldier.prototype.walk = function(){ /*code for walking two steps*/} Soldier.prototype.run = function(){ /*Running code*/ } soldier.prototype.death = function(){ /*Go die*/ } Soldier.prototype.attack = function(){ /*Brush his bear face*/ } Soldier.prototype.defense = function(){ /*protect face*/ } Or you can reassign Soldier.prototype = { constructor: Soldier, Arms: "American Soldier", Attack power: 5, Walking: function(){ /*code for walking two steps*/}, Run: function(){ /*Running code*/ }, Death: function(){ /*Go die*/ }, Attack: function(){ /*Brush his bear face*/ }, Defense: function(){ /*Protect the face*/ } } This is the end of this detailed article about the role of new in JS. For more information about the role of new in JS, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Docker image creation Dockerfile and commit operations
>>: MySQL character types are case sensitive
The use of vue3 Teleport instant movement functio...
Cookie It is a standard way to save the state of ...
①. How to use the alias (CNAME) record: In the do...
1- Styling dropdown select boxes - Modify the dro...
Nginx can use the limit_req_zone directive of the...
In the process of Django web development, when wr...
In the previous article, we played with timeouts ...
Conclusion: In a multithreaded environment, if on...
Table of contents 1. Mathematical functions 2. St...
MySQL full text search Chinese solution Recently,...
Table of contents Introduction Mirror repository ...
Table of contents Preface analyze Data Total Repe...
Greek letters are a very commonly used series of ...
System environment: Ubuntu 16.04LTS This article ...
1.1 Copy the nginx installation package and insta...