0

I have the following code:

    var dataArray = [];

    switch (request_url.query.group) {
        case 'returning': 
            dataArray = {};
            var returning = _.filter(result, function(result) {
                return (result.sessions.length > 1) ? true : false;
            });
            //Am I setting the object dataArray here?
            dataArray.returning = returning.length; 
            //Am I setting the array dataArray here?
            dataArray['new'] = result.length - returning.length; 
            break;

I have confusion regarding dataArray here. The first line of code declares an array and in the switch case an object of the same name is declared. What is the 'new' and 'returning' values set and whom do it respectively belong to, the array or the object? Also, is object a super class of array in Node.js?

3
  • 1
    There's only one declaration, on the first line. Commented Apr 16, 2013 at 22:52
  • There is a declaration of dataArray in the switch case Commented Apr 16, 2013 at 22:57
  • 1
    no there isn't. There's an assignment. Commented Apr 16, 2013 at 23:05

1 Answer 1

1

What is the 'new' and 'returning' values set and whom do it respectively belong to, the array or the object?

They'll belong to the Object. The Array is only referenced by dataArray up until this line:

dataArray = {};

After that, dataArray will only be a reference the Object while the Array would become unreachable and available for garbage collection.


Note: If you were wanting dataArray to be an "Array of Objects," you can assign the Object to an index of dataArray:

dataArray[0] = {};

And set the properties similarly:

dataArray[0].returning = ...;
dataArray[0]['new'] = ...;

Also, is object a super class of array in Node.js?

Yes. Arrays inherit from Objects because Array.prototype is an Object. MDN has a good summary of the prototype chain, which is JavaScript's inheritance model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.