官术网_书友最值得收藏!

Differences between JavaScript coding using Node and in the browser

Inside the browser, you've probably used window if you've done any JavaScript development:

Window is the global object, it stores basically everything you have access to. In the following screenshot, you can see things such as array, we have all sorts of CSS manipulation and Google Analytics keywords; essentially every variable you create lives inside Window:

We have something similar inside Node called global, as shown here:

It's not called window because there is no browser window in Node, thus it is called global. The global object stores a lot of the same things as window. In the following screenshot, you can see methods that might be familiar, such as setTimeout and setInterval:

If we look at this code screenshot, we have most of the things that are defined inside the window, with some exceptions, as shown in the following screenshot:

Now, inside the Chrome browser, I also have access to document:

The document object stores a reference to the Document Object Model (DOM) in the Node website. The document object shows exactly what I have inside the browser's viewport, as shown in the following screenshot:

I can make changes to the document to update what gets shown up on the browser's viewport. Now, obviously we don't have this HTML document inside Node, but we do have something similar, which is called process. You can view it by running process from Node, and in the following screenshot, we have a lot of information about the specific Node process that's being executed:

There's also methods available here to shut down the current Node process. What I'd like you to do is run the process.exit command, passing in as an argument the number zero, to say that things exited without error:

process.exit(0);

When I run this command, you can see I'm now back at the command prompt, as shown in the following screenshot:

I've left Node, and I'm at a place where I can run any regular command prompt command, such as checking my Node version. I can always get back into Node by running node, and I can leave it without using the process.exit command by using control + C twice.

Now, I'm back at my regular command prompt. So, these are the notable differences, obviously inside the browser you have the viewable area, window gets changed to global, and a document basically becomes process. Now, obviously that's a generalization, but those are some of the big picture changes. We'll be exploring all the minutiae throughout the book.

Now, when someone asks you what is Node? You can say Node's a JavaScript runtime that uses the V8 engine. When they ask you what the V8 engine is, you can say the V8 engine is an open source JavaScript engine written in C++ that takes JavaScript code and compiles it to machine code. It's used inside Node.js and it's used in the Chrome browser.

主站蜘蛛池模板: 临沭县| 封丘县| 洪江市| 民权县| 衡水市| 凤山县| 伊川县| 八宿县| 曲阜市| 开江县| 洪洞县| 浦北县| 大化| 平乡县| 林口县| 嘉鱼县| 松阳县| 郑州市| 安阳市| 中山市| 宝山区| 南安市| 即墨市| 佛坪县| 增城市| 阿城市| 锦屏县| 平安县| 鹤岗市| 玉溪市| 平山县| 厦门市| 泸州市| 蒙山县| 普陀区| 阿坝| 昔阳县| 景洪市| 勃利县| 东乡| 中卫市|