Skip to content

Deviations from JavaScript

Erin Bennett edited this page Jul 30, 2015 · 9 revisions

The order in which functions are defined matters in the presence of wrappers/decorators

For example, this works as expected:

var f = function() { return g() };
var g = function() { return 42 };
f(); // => 42

But wrapping f breaks things:

var id = function(x) { return x; };
var f = id(function() { return g() });
var g = function() { return 42 };
f(); // "Uncaught ReferenceError: g is not defined"

See #61.

Variable scope

In JS, the consequent and alternate blocks of an if statement don't introduce new scopes. They do in WebPPL:

if (true){
  var x = 1;
} else {
  var x = 2;
}
x // "Uncaught ReferenceError: x is not defined"

See #103.

arguments not always available

In some situations the built-in arguments may not be available for the entirety of the function body:

var g = function() {};

var f = function() {
  g();
  return arguments;
};

f(1,2,3); // => [], rather than [1,2,3]

See #31.

Can't pass primitives to higher-order functions

Higer-order functions written in WebPPL don't work when passed primitive JS functions. Perhaps less importantly, higher order JS primitives won't work when passed WebPPL functions.

See #30.

Function declarations (statements) not supported

Functions can only be created with function expressions.

See #2.

Implicit return

WebPPL functions sometimes (always?) implicitly return the value of the last expression evaluated:

var f = function() {
  if (false) {
    0
  } else {
    1
  }
};

f() // => 1

Applying anonymous functions requires parentheses

In JavaScript, the parentheses around the anonymous function below are optional, but in WebPPL, parentheses around the anonymous function are required. So the following will return 3 only when there are parentheses. Without them, the program will not run.

(function() {
  return 3;
})()
Clone this wiki locally