13

I'm reading this great book called Eloquent JavaScript but I'm confused by the use of the word "binding" in this example:

It is possible to include symbol properties in object expressions and classes by using square brackets around the property name. That causes the property name to be evaluated, much like the square bracket property access notation, which allows us to refer to a binding that holds the symbol.

let stringObject = {
  [toStringSymbol]() { return "a jute rope"; }
};
console.log(stringObject[toStringSymbol]());
// → a jute rope

As I understand it (so far in my JS journey), "binding" relates to specifying which this or object context in which a function operates. See here.. Binding is perhaps something related to context. That is why we have .bind().

But in this example we are binding something else (a method whose key is a symbol). Does binding just mean attaching a property (primitive or method) to an object?

cham
  • 8,666
  • 9
  • 48
  • 69
  • See also [Is a variable declaration the same as a variable's binding?](https://stackoverflow.com/q/50898687/1048572) – Bergi Apr 17 '22 at 13:13

3 Answers3

11

Does binding just mean attaching a property (primitive or method) to an object?

No

Your previous paragraph provides a better explanation:

"binding" relates to specifying which this or object context

Sort of

Everything tracked by JavaScript is bound. In fact, the definition of undefined means JavaScript cannot find a bound identifier.

Answer

Binding something in JavaScript means recording that identifier in a specific Environment Record. Each Environment Record is related to a specific Execution Context - and that binds the identifier (variable or function name) to the this keyword for that execution context.

Reference

https://www.ecma-international.org/ecma-262/5.1/#sec-10.5

Less Formally

Think of Environment Records as buckets of stuff. These are not Objects or Functions or Variables or anything we code in JavaScript, these buckets contain all these things. There are many buckets in a JavaScript application. Each bucket operates independently from the other buckets. That independence is represented as a Context (or Execution Context) in JavaScript. But sometimes we want to use stuff from one bucket inside a different bucket. That is where binding comes in. We can bind stuff from one bucket into the context of a different bucket for execution there. (A side effect of doing all that is the this keyword reflects the bucket borrowing the stuff).

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • 2
    This is great info, thanks. It's unfortunate that Eloquent Javascript made this change when it went from 2nd to 3rd edition. It was such a great teaching tool when it referred to variables as _variables, like every other resource_. What a confusing and unnecessary change for a book targeting unexperienced developers. This info could have easily been included in a chapter explaining `.bind()` and `this` etc. – aremay Mar 26 '19 at 14:15
  • @reustin Agreed. I ended up here from the same resource and the same reason, having previously learnt ES5 a while ago, and the universal references to bindings gave me the impression that variables had been renamed in ES6. – Hashim Aziz Sep 29 '20 at 23:07
7

A binding in JavaScript is the formal terminology for what a lot of people refer to as a variable. In ES2015+, a variable can be defined with the let keyword, but you can also define constant with the const keyword. A binding could refer to either a variable or a constant.

Reference: See chapter 2, page 1 of Eloquent JavaScript, under the section heading 'Bindings' (https://eloquentjavascript.net/02_program_structure.html)

L H
  • 1,145
  • 3
  • 12
  • 25
-2

In this book, they use binding or variable interchangeably. So simply means, a binding is a variable. I guess you started reading a book by skipping the pages, don't do it again. I will write a very simple program to illustrate.

let count = 1;//count is a binding(a variable)
++count;//it's called side effect(because the original value of count binding was modified)
Visal Ing
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 11 '22 at 19:58