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?
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. Think of Environment Records as buckets of stuff.
In this syntax, the bind () method returns a copy of the function fn with the specific this value ( thisArg) and arguments ( arg1, arg2, …). Unlike the call () and apply () methods, the bind () method doesn’t immediately execute the function. It just returns a new version of the function whose this sets to thisArg argument.
Using JavaScript bind () for function binding. When you pass a method an object is to another function as a callback, the this is lost. For example: let person = { name: 'John Doe' , getName: function() { console .log ( this .name); } }; setTimeout (person.getName, 1000 ); Code language: JavaScript (javascript)
The bind () method creates a new function, when invoked, has the this sets to a provided value. The bind () method allows an object to borrow a method from another object without making a copy of that method. This is known as function borrowing in JavaScript. Was this tutorial helpful ?
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With