-1

I'm looking at the following node.js code:

Chrome(function (chrome) {
    with (chrome) {
        on('Network.requestWillBeSent', function (message) {
            console.log(message.request.url);
        });
        on('Page.loadEventFired', close);
        Network.enable();
        Page.enable();
        Page.navigate({'url': 'https://github.com'});
    }
}).on('error', function () {
    console.error('Cannot connect to Chrome');
});

which is taken from here. My question is what is the "with" keyword where it says "with (chrome)" I can't find it in the api.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
user3792159
  • 111
  • 2
  • 13

1 Answers1

2

It doesn't stand for anything, it means "with" in the sense of the English word.

It isn't part of the Node API, it is core JavaScript.

See the EcmaScript specification and MDN.

Adds the given object to the scope chain used when evaluating the statement. The parentheses around object are required.

It basically means that for any variable, after checking for a local variable and before checking the default object (which would be window is this was client side JS in a webpage) for a property of that name, it will check the object in chrome for a property of that name.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 4
    It should be noted that use of `with` is universally frowned upon as it tends to make code less understandable. – Rob Raisch Jul 01 '14 at 23:21
  • 1
    Also, *with* statements [*aren't allowed*](http://ecma-international.org/ecma-262/5.1/#sec-12.10.1) in strict mode code. – RobG Jul 01 '14 at 23:26