简体   繁体   English

为什么在节点更改时将原型中的var node = this.obj辅助而不是this.obj?

[英]Why does assiging var node= this.obj inside a protoype not this.obj when node changes?

I'm implementing a simple linked list in javascript using prototypes. 我正在使用原型在javascript中实现一个简单的链接列表。 I came across something I don't quite understand - 我遇到了我不太了解的事情-

var Node = function( value ) {
  this.value = value;
  this.next = null;
};

var List = function( head ) {
  this.head = null;  
};

List.prototype.insert = function( value ) {
   if ( this.head === null ) {
       this.head = new Node( value ); 
   } else {
     var aNode = this.head;
     console.log( 'same: ' + (aNode === this.head)); // prints true
     while ( aNode.next !== null ) {
       aNode = aNode.next;
     }
     var node = new Node( value );
     aNode.next = node;
     console.log( 'Head: ' + this.head.value );  //prints 1
     console.log( 'Node: ' + aNode.value );  //prints 2,3,4
   }
};

var aList = new List();
aList.insert( 1 );
aList.insert( 2 );
aList.insert( 3 );
aList.insert( 4 );

If this.head and aNode share a reference, changing aNode to aNode.next doesnt change this.head. 如果this.head和aNode共享引用,则将aNode更改为aNode.next不会更改this.head。 Can someone explain why? 有人可以解释为什么吗? I'm new to prototypes. 我是原型的新手。

Because of the order of operations. 由于操作顺序。 You want parentheses on that: 您想要在其上加上括号:

console.log( 'same: ' + (aNode === this.head))
// ---------------------^-------------------^

Without them, it's effectively 没有他们,这是有效的

console.log( ('same: ' + aNode) === this.head)

...(which is, of course, false) because + has a higher precedence than === . ...(这当然是假的),因为+优先级高于=== It's the same reason that if (a + 5 === 6) is true when a is 1 . 同样的原因是当a1if (a + 5 === 6)为true。

In your console.log it is first evaluating the concatenation of same string and aNode and then comparing to this.head : console.log它首先评估same字符串和aNode的串联,然后与this.head进行比较:

Change it to: 更改为:

console.log( 'same:' + (aNode === this.head))

or: 要么:

console.log( 'same:', aNode === this.head)

This should do the trick... let me know. 这应该可以解决问题...让我知道。

  1. Declare a new list, example --- var chores = new List("chores"); 声明一个新列表,例如--- var chores = new List("chores");
  2. Declare a new list item, example --- chores.add("Mow lawns."); 声明一个新的列表项,例如--- chores.add("Mow lawns.");

//CODE //码

<!DOCTYPE html>
<html>
<head>
<script>
//LIST (OBJECT CONSTRUCTOR)
var List = function(title){
    this.title = title;
    this.datetime = new Date(); 
    this.items = []; 
};

//LIST (OBJECT METHOD - ADD)
//ALL OBJECTS CREATED FROM THE LIST OBJECT CONSTRUCTOR ABOVE INHERIT THIS METHOD
List.prototype.add = function(val){
    this.items.push(val);
};

//CREATE NEW LIST OBJECT CALLED (CHORES)
var chores = new List("chores");

//INPUT DATA USING LIST METHOD ADD, WHICH OBJECT CHORES INHERITED WHEN IT WAS INSTANTIATED (CREATED)
chores.add("Mow lawns.");
chores.add("Make dinner.");
chores.add("Drive to the store.");

//VIEW OUTPUT
console.log(chores);
console.log(chores.items);
console.log(chores.items[0]);
console.log(chores.items[1]);
console.log(chores.items[2]);
</script>
</head>
<body>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM