Using property names beginning with an underscore ("_")

I have this in my Node script (simplified for this question):

var http = require("http"),
    url = require("url"),
    options = url.parse("http://example.com/"),
    moreOptions = url.parse("http://example.net/"),
    callback,
    request,
    moreDifferentRequest;

callback = function( res ) {
    console.log(res.socket._httpMessage._headers.host);
}

request = http.request( options, callback );
request.end();

moreDifferentRequest = http.request( moreDifferentOptions, callback );
moreDifferentRequest.end();

I grab the host name from res.socket._httpMessage._headers.hostbecause it does not exist in res.headers.

But underscores give me a break. They tell me: "Hey, this should be considered as private property for internal use only socket, so do not read it because it can completely change in a later version, and we are not going to tell you.

If I am right to think that I am doing it wrong, what is the correct way to get the hostname inside the callback? Or am I just completely misinterpreting the underscores, and what am I doing is fine?

+5
source share
1

, , . , , API, . , , - .


, :

options.host

HTTP- , (), .


:

var http = require("http"),
    opts = require("url").parse( "http://stackoverflow.com" );


function wrapper( options, callback ) {
    return function() {
        args = [].slice.call(arguments);
        args.push( options );
        return callback.apply( this, args );
    }
}

request = http.request( opts, wrapper(opts, function(response, options){
    console.log( options.host );
}));

request.end();
+3

All Articles