0

I have this code that allows me to return a string about the posts connected to a particular post. I tried to use the bracket and dot notation in accessing properties below in the list part (list=list+this["connectionsTo"[i]].postNum+"\n";) but doesn't work. But then when I used the double dot notation, it worked, why? I believe those two are the same.

function Fencepost(x, y, postNum) {
  this.x = x;
  this.y = y;
  this.postNum = postNum;
  this.connectionsTo = [];
}

Fencepost.prototype = {
  sendRopeTo: function(connectedPost) {
    this.connectionsTo.push(connectedPost);
  },
  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;
  },
  movePost: function(x, y) {
    this.x = x;
    this.y = y;
  },
  valueOf: function() {
  return Math.sqrt(this.x * this.x +
                   this.y * this.y);
  }
};

Fencepost.prototype.toString=function(){
var list="";
for(var i=0;i<this.connectionsTo.length;i++){
list=list+this["connectionsTo"[i]].postNum+"\n";
}
  return "Fence post #"+this.postNum+":\nConnected to posts:\n"+list+"Distance from ranch: "+this.valueOf()+" yards";
};

var post10=new Fencepost(3,4,10);
var post11=new Fencepost(5,7,11);
var post12=new Fencepost(6,7,12);
var post13=new Fencepost(8,9,13);
post10.sendRopeTo(post11);
post10.sendRopeTo(post12);
post10.sendRopeTo(post13);
console.log(post10.toString());
  • 1
    "doesn't work". Ok. What error did you get? Check the javascript console when you try to execute this code and report back what you find. – Joseph Marikle Apr 26 '17 at 13:13
  • What is "*double dot notation*"? – RobG Apr 26 '17 at 13:15
  • "anything"[n] isn't going to work the way you expect. Not sure what double dot notation is. – Dave Newton Apr 26 '17 at 13:15
  • Hi mister I'm new to JS (started 10 days ago) and this is what I find in JS Bin:"error" "TypeError: Cannot read property 'postNum' of undefined at Fencepost.toString (hezecawilo.js:38:35) at hezecawilo.js:50:45 at https://static.jsbin.com/js/prod/runner-3.41.10.min.js:1:13926 at https://static.jsbin.com/js/prod/runner-3.41.10.min.js:1:10855" –  Apr 26 '17 at 13:15
  • `"connectedTo"[i]` evaluates to the i-th character of "connectedTo" – le_m Apr 26 '17 at 13:15
  • ya mister I edited the question, that was supposed to be ["connectionsTo"[i]].postNum, still doesnt work –  Apr 26 '17 at 13:16
  • 1
    `"connectionsTo"[i]` will return a character at the i position. I believe you wanted `this["connectionsTo"][i]` – bzim Apr 26 '17 at 13:18
  • `property 'postNum'` is missing because `this[c] .. this[o] .. this[n]` are all undefined. You're trying to get `this["connectionsTo"][i]`, but as Bergi pointed out, you're getting the individual characters of the string `connectionsTo` instead. – Joseph Marikle Apr 26 '17 at 13:19
  • Thank you :D. I tried it and it worked :) –  Apr 26 '17 at 13:20
  • The logic of *removeRope* is broken. It assigns a new array to *this.connectionsTo*, but any existing reference to that property will continue to reference the old (unmodified) array. You should *splice* the match from *connectionsTo*, not create and assign a new array from the non-matches (which is illogical, inefficient and likely the source of future obscure bugs). – RobG Apr 26 '17 at 13:25

1 Answers1

4

Since you are iterating over the this.connectionsTo array, you are looking for

this["connectionsTo"][i]

or the equivalent

this.connectionsTo[i]

instead of

this["connectionsTo"[i]]

which indexes the string, and then takes that character for the property of this.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @MarkLemuelGenita The edit concerns `"connectedTo"` vs `"connectionsTo"`, which is not what I am talking about – Bergi Apr 26 '17 at 13:19
  • a follow-up question mister, why sometimes JS Bin requires me to use bracket and dot notation (accessing a property of an object within an object) and sometimes it doesn't? –  Apr 26 '17 at 13:26
  • See [the question](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) I had linked in my answer. You can always use bracket notation, but for constant identifier-named properties dot notation is easier to read. – Bergi Apr 26 '17 at 13:28