Constructor I

So now the cowboy-devs are so excited about their robotic fence posts that they’ve decided to go ahead and make a constructor function for fence posts, to which they can pass a few parameters and get back a new instance of a post.

And, of course, by making a new constructor function, they mean YOU will make it.

Your job is to take your existing ‘genericPost’ and rebuild all of its properties inside a single Fencepost constructor (we’ll worry about inheritance in the next challenge). The ‘genericPost’ is provided for you:

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);
     }
   }
  };
Your constructor should take in parameters with names that shadow the property names in genericPost, with one exception. The cowboy-devs have asked you to set the connectionsTo property initially to be an empty array inside the constructor (no parameter needed). Then, modify the sendRopeTo function in a way that EXPECTS an array to already be present, rather than having to make one as it currently does when it finds an undefined value. This, by the way, will make the function much smaller.

Remember, for now, you’ll also build any functions as properties directly within the constructor.
Constructor II

Now that you’ve got a Fencepost constructor, let’s make some posts!

The cowboy-devs need three new posts for the Plains, described below:

x: -3, y: 4
numPost: 18

x: 5, y: -1,
numPost: 19

x: -2, y: 10
numPost: 20.

Use your constructor to make these fence posts, naming them post<number> as before. Then connect them with the connection function using the following criteria.

If two posts both have even y-coordinates, they should be connected.
If two posts both have odd x-coordinates, they should be connected.
Your constructor is provided for you.
Constructor III

So now that’s there’s eleventy-billion fence posts everywhere, the cowboy-devs have noticed a significant drain on their memory resources. They’d like you to take a look around the Fencepost constructor and see if there’s anything you can add to a prototype, so that every stinkin’ fence post doesn’t have to carry around anything that it could get from just one place.

Below is the current status of the constructor, with some additions the cowboy-devs have made to improve functionality of the fence post objects. Your job is to identify the portions of the constructor that should be available to ALL fenceposts, and put those in a prototype for fence posts. Your answer should include the modified constructor as well as the newly designed prototype for that constructor. Good luck, pardner.

function Fencepost (x, y, postNum){
  this.x = x;
  this.y = y;
  this.postNum = postNum;
  this.connectionsTo = [];
  this.sendRopeTo = function ( connectedPost ){
    this.connectionsTo.push(connectedPost);
  };
  this.removeRope = function ( removeTo ){
  var temp = [];
  for(var i = 0; i<this.connectionsTo.length; i++){
     if(this.connectionsTo[i].postNum != removeTo){
       temp.push(this.connectionsTo[i]);
     }
  }
    this.connectionsTo = temp;
  }
  this.movePost = function (x, y){
    this.x = x;
    this.y = y;
  };
}