this' different between REPL and script
Node's REPL
is global. Code from a file is in a "module", which is really just a function.
Your code file turns into something like this very simplified example:
var ctx = {};
(function(exports) {
// your code
console.log(this === global);
}).call(ctx, ctx);
Notice that it's executed using .call()
, and the this
value is set to a pre-defined object.
Why are there differences between the node REPL and node script running when evaluating these expressions
It all has to do with how a statement is parsed. A statement (not an expression) that begins with a {
is a statement block. When {
appears in an expression, it introduces an object literal.
The statement:
{ foo: 'bar' }
is syntactically correct but semantically different when interpreted as a statement and when interpreted as an expression. In the former case, it's the block statement:
{
foo: 'bar'
}
which is a block containing one statement, the labeled expression 'bar'
. It is not an object literal.
The expression {...a}
fails when {
introduces a statement block because ...a
cannot itself be parsed as a statement.
What is the difference between a REPL and an interpreter?
Interactive interpreters use REPLs. An interpreter is not required to have one. You can run Python, for example, in non-interactive mode (on a file) and it will not use a read-eval-print loop.
In Python REPL, what is the difference between using a variable and printing it?
IDLE is an example of a Read-Eval-Print Loop, AKA REPL. You type a statement, it executes it, and if it's an expression with a value it prints the value.
- You call a function, you use or assign a variable.
So in [2] you used the variablea_tuple
. IDLE evaluated it and printed its value. - In [8] it's printing the representation of
ndarray
, which is a numpy array. The representation of numpy arrays is shown as the contents in square brackets insidearray()
. [1, 2, 3]
is a list, being passed as the argument to thenumpy.array()
function. numpy will then create an array whose contents are the elements of the list.- You can convert the array to a list with
ndarray.tolist()
. - The only difference between [3] and [8] is the type of value that you're evaluating and printing. [3] is a list, [8] is an array. Different types are shown in different ways.
- They're Python statements.
- Yes, they're output. More specifically, they're the values of the expressions that you typed on the previous lines.
- Yes, it's code. The purpose of an REPL is to allow you to execute code interactively, as opposed to running code in a script file.
Why I got different result out of console.log(this) from REPL
Different execution contexts.
In node, any file you require is a module that has its own scope, by default an empty object (hence the {}), until you export something.
The repl, on the other hand, is its own execution context with a bunch of stuff already attached to it. You can even set things up to attach to it yourself (e.g. Convenient functions)
How do Lua syntax rules differ between REPL and scripts?
The Lua REPL tries to evaluates input as expressions.(*) If it succeeds, it prints the results. This is for convenience only.
(*) It does that by prepending return
to the input and trying to run that as a script.
Related Topics
How to Access the Http Request Header Fields via JavaScript
How Can D3.Transform Be Used in D3 V4
Overriding Browser's Keyboard Shortcuts
Websocket Handshake with Ruby and Em::Websocket::Server
Wkwebkit JavaScript Execution When Not Attached to a View Hierarchy
Creating a "Sticky" Fixed-Position Item That Works on iOS Safari
Push Is Overwriting Previous Data in Array
Copy Output of a JavaScript Variable to the Clipboard
Finding All Indexes of a Specified Character Within a String
Safari on iOS 9 Does Not Trigger Click Event on Hidden Input File
Firebase.Database Is Not a Function
How to Execute Array of Promises in Sequential Order
How to Compile Ruby to JavaScript
JavaScript Loops: For...In VS For
Performance of Key Lookup in JavaScript Object
Smart Way to Truncate Long Strings