This' Different Between Repl and Script

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.

  1. You call a function, you use or assign a variable.
    So in [2] you used the variable a_tuple. IDLE evaluated it and printed its value.
  2. 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 inside array().
  3. [1, 2, 3] is a list, being passed as the argument to the numpy.array() function. numpy will then create an array whose contents are the elements of the list.
  4. You can convert the array to a list with ndarray.tolist().
  5. 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.
  6. They're Python statements.
  7. Yes, they're output. More specifically, they're the values of the expressions that you typed on the previous lines.
  8. 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



Leave a reply



Submit