简体   繁体   中英

What does binding mean in Javascript?

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?

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).

Unfortunately, the accepted answer is wrong in the context of this question.

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.

To catch and hold values, JavaScript provides a thing called a binding, or variable.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM