0

I do not understand such snippet of code. is there any master can help me explain that: I already search the explanation of querystring.parse(str, [sep], [eq], [options]). However, I do not understand why there is a "= function (str){}" after it. thanks Here is the code:

(function () 
 {
    'use strict';
    var queryString = {};
	
    queryString.parse = function (str) //do not know why there is a function assigned here
	{
        if (typeof str !== 'string') 
		    {
            return {};
        }

        str = str.trim().replace(/^\?/, '');

        if (!str) {
            return {};
        }
  }
});

2
  • 1
    In JS Function is a full-blown Object. Commented Mar 11, 2019 at 21:45
  • 1
    You've got a function expression (the outermost part of your code) which isn't assigned anything or called. If it was called, then the queryString object would be created, have the parse method added, and then the entire thing would drop out of scope without being used. So the entire thing does nothing. So I've no idea why any of it exists. Commented Mar 11, 2019 at 21:50

1 Answer 1

1

It's just saying that it is a function and it will run the code inside the curly brackets when called. In this case queryString is not the node module, since it was not required and assignet to that value(like one would with const querystring = require('querystring')), instead it is just an empty object (as you can see in var queryString = {})

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

1 Comment

You explained very well. I now understand. Thank you so much and appreciate it.

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.