0

Is there a specific reason to use "that?"

Note about using "that": I found a thread explaining "that" :What does 'var that = this;' mean in JavaScript?. One of the respondants said: "By convention, we make a private that variable. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions." Thus, I am confused if the "that" here has any special usage.

In addition, Why not simply assign a var name to replace that.push? Thank you very much.

var LinkedList = function(e){

  var that = {}, first, last;

  that.push = function(value){
    var node = new Node(value);
    if(first == null){
      first = last = node;
    }else{
      last.next = node;
      last = node;
    }
  };

  that.pop = function(){
    var value = first;
    first = first.next;
    return value;
  };
  var Node = function(value){
    this.value = value;
    var next = {};
  };

  return that;
}; 
Community
  • 1
  • 1
LED Fantom
  • 1,229
  • 1
  • 12
  • 32
  • 1
    2 is a javascript object, with the firstName and lastName properties. – Zack Jan 13 '15 at 22:55
  • Javascript objects can be considered as key/value arrays. http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/ – imnancysun Jan 13 '15 at 22:56
  • More correctly, javascript Arrays are Objects with a special length property. Objects are simple collections of key/value pairs, as are Arrays. Neither have any sense of order. – RobG Jan 13 '15 at 22:58
  • The LinkedList object is trying to implement a queue in javascript. More info here http://stackoverflow.com/questions/1590247/how-do-you-implement-a-stack-and-a-queue-in-javascript – Zack Jan 13 '15 at 22:59
  • I edited my question to be more specific. Any help will be greatly appreciated. :) – LED Fantom Jan 14 '15 at 18:26

3 Answers3

2

(1) is array of string

Yes, that is correct. However, as there is no strict typing in Javascript a better description would be an array containing strings. The array isn't limited to containing strings just because it is created with only strings in it.

(2) is array of object

No, that's not correct. It's an object, so it contains properties. Each property is an name and a value.

An object is not an array, but an array is actually an object. An array can have items, but it can also have named properties.

The LinkedList function creates an object that has push and pop methods just like an array, but the items are stored differently than in an array.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

As noted above, (2) is a javascript object with properties, and therefore doesn't contain standard array methods of push and pop. The code is creating a custom object called LinkedList, that has push/pop behavior, but couldn't be an array as it would conflict with the methods of an array.

jookyone
  • 141
  • 7
0

Question 1: … (2) is array of object. Is that right?

No. As commented by Zack, #2 assigns an Object to the x variable.

Question 2: Assuming what I understood is right above,

Unfortunately it isn't.

Could someone explain why the code below wants to create that.push and [that.pop] and then store them in the that object ---- that = {}?

The code assigns an Object to that. Objects don't have push or pop methods. The design apparently calls for the object returned by LinkedList to have such methods, so they are added.

RobG
  • 142,382
  • 31
  • 172
  • 209