Creation with Prototypes I

The cowboy-devs at the Plains of Prototypes admire your excellent work with their landscape’s namesake. They have a new problem for you to examine, one that they are really excited about. For the first time, they have a new robotic fence post system for their pastures. This system is pretty cool, because it allows immediate shifting of the wheel-based fence posts to make a forced migration of the cows into different, healthier pastures for grazing.

Here’s the generic fence post object, starting at coordinates (0,0), which is the location of the main ranch:

var genericPost =
  {x: 0, y: 0, postNum: undefined,
   connectionsTo: undefined,
   sendRopeTo: function ( connectedPost ) {
     if(this.connectionsTo == undefined){
     var postArray = [ ];
     postArray.push(connectedPost);
       this.connectionsTo = postArray;
     } else {
       this.connectionsTo.push(connectedPost);
     }
   }
  };
Each new fence post will travel, on its own, out to the coordinates assigned to it when a new object for it is generated. There it will send out robotically guided connection ropes to other, appropriately designated fence posts to enclose a pasture. Bear in mind that it takes two ropes to for fence posts to be connected; i.e. each sends a rope to the other. Examine the prototype closely to see how the system is designed to take care of that.

To test the creation of these new fence posts objects, the cowboy-devs have a few requirements:

Use the Object.create method and the base fence post as a prototype to create two new fence posts, post1 and post2.
Send the first fence post to (-2, 4) using standard property modification with dots only. Also set its number to 1.
Send the second fence post to (5, 1) using standard property modification with dots only. Also set its number to 2.
Use the method inherited from the prototype to connect the fence posts together. Each connectionsTo array should receive an entire object.
Build some posts, (wo)man!
Creation with Prototypes II

Turns out that there are few fence posts that have some special properties. Some have weathervanes, some have birdhouses, and some even have emergency lights.

Below are the data for three posts that need to be created using the genericPost as a prototype (as in the last challenge). The cowboys have given you all the data you’ll need to build each using the prototype and then assign unique property values through modification. Call each of your posts post<number>, just like you did in the last challenge.

x: 0, y: -3,
postNum: 8,
connectionsTo: 10

x: 6, y: 8,
postNum: 9,
connectionsTo: 10

x: -2, y: 3,
postNum: 10,
connectionsTo: 8, 9

The cowboy-devs have prepared a list of additions that need to happen for certain special fence posts. After you’ve built the above three posts, add properties to those post where the cowboy-devs have deemed appropriate.

Any fence posts with an even ‘y’ coordinate have a birdhouse, and therefore have a numBirds property initially set to 0.
Any fence posts connected to Post #9, but are not Post #9, have a property of weathervane initially set to “N”.
Even numbered fence posts have emergency lights, and a lightsOn property initially set to false.
The base fencepost is again provided for your reference. There are many single lines of code to be entered on this one, so be careful to assign all properties necessary.