小编Mar*_*ita的帖子

Javascript原型覆盖

我有这个代码,允许我返回一个关于连接到特定帖子的帖子的字符串.我试图在列表部分(list=list+this["connectionsTo"[i]].postNum+"\n";)中访问下面的属性时使用括号和点表示法,但不起作用.但是当我使用双点符号时,它有效,为什么?我相信那两个是一样的.

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); …
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
113
查看次数

标签 统计

javascript ×1