Javascript / NodeJS: (() => 0) === (() => 0)

I am reading Javascript Allongé and in it I can see this code that should return false:

(() => 0) === (() => 0)

      

When I run this on the command line (Ubuntu 14.04) using Nodejs, I get three dots:, ...

after which I cancel CTRL-C.

I run node.js with the following command: nodejs

rather than node

. The use --harmony

doesn't matter.

Node.js version: v0.10.25

Why don't I return the result? I was thinking about using Nodejs as a command line testing utility, but maybe this is not a good idea?

+3


source to share


2 answers


Remove nodejs:

sudo apt-get remove nodejs

      

Install NVM (Node Version Manager). Check the Github page for the latest version and copy the correct command there. This is for NVM 0.25.4 version:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.25.4/install.sh | bash

      

Logout, login and nvm

should work. Then install the required version of node.js, possibly multiple versions next to each other:



nvm install 0.12.5

      

When this is complete, you can start node using the harmony parameter:

node --harmony

      

Now the following code should return false

:

(() => 0) === (() => 0)

      

0


source


The simple answer as to why it returns false

is that while functions do the same thing and look the same, they are initialized in two different memory locations (for objects like functions, simple objects, and arrays, the strict equality operator ===

checks the location memory of this object.)

Also, if you are going to use Node.js you have to make sure it interprets it as ECMAScript6 (otherwise () => 0

invalid ES5 which is the default for Node.js).

You can use a flag --harmony

:

node --harmony app.js

      

For more information on using ES6 in Node.js see this question on what to node --harmony

do? "
A short quote from the top answer:

harmony seems to allow new ECMA features in this language. The reason your file won't work without negotiation is because app.js is probably using non-backward compatible features from the new ECMA standard (e.g. scope, proxies, sets, maps, etc.)



To explain why you can see three dots, see this question :

So you opened node in an interactive terminal and then typed node example.js, so it tries to run that as if it were JavaScript. It shows three dots because this is invalid JavaScript code and it is waiting for you to type more code that might make it valid.

output

Above is my conclusion. This is what I was running in my terminal (using 0.12.21

):

> $ node --harmony
> (() => 0) === (() => 0)
> false

      

+6


source







All Articles