简体   繁体   中英

What does the term 'binding' mean in JS?

Please clarify for me what the term binding means in JavaScript. I've started to read the book 'Eloquent JS' and there are lots of occurrences of this word. Does it just mean variable? Here are few examples from the book:

  1. So if you know that the property you are interested in is called color, you say value.color. If you want to extract the property named by the value held in the binding i, you say value[i].Property names are strings. They can be any string, but the dot notation works only with names that look like valid binding names.
  2. Bindings can also be changeable or constant, but this is separate from the way their values behave. Even though number values don't change, you can use a let binding to keep track of a changing number by changing the value the binding points at. Similarly, though a const binding to an object can itself not be changed and will continue to point at the same object, the contents of that object might change.

I've found out what it is (in this book in an earlier chapter). Here is the snippet:

How does a program keep an internal state? How does it remember things? We have seen how to produce new values from old values, but this does not change the old values, and the new value has to be immediately used or it will dissipate again. To catch and hold values, JavaScript provides a thing called a binding, or variable:

 let caught = 5 * 5;

That's a second kind of statement. The special word (keyword) let indicates that this sentence is going to define a binding. It is followed by the name of the binding and, if we want to immediately give it a value, by an = operator and an expression.

The previous statement creates a binding called caught and uses it to grab hold of the number that is produced by multiplying 5 by 5.

Binding is a general term to mean that your symbol or variable points to some location in memory. This is also illustrating the principle in JavaScript that even when you declare an object using const, you can still manipulate the properties of that object. Only the reference to the object itself cannot be redefined.

So you can't say myObj = {greeting: "Hello Stack Overflow"} and then myObj = someOtherObj. But you can say myObj.greeting = "Beep Boop"

Okay, let me clarify. The first question what is a binding:

  1. A binding is a symbol, variable, constant, etc. that points to some literal value, or object in memory.

  2. In reference to your book, it says:

     const myObj = { color: blue }; // the property color is defined on this object myObj["color"]; // => "blue" The color property can be accessed by passing in a string myObj = anotherObj; // not allowed, you declared it with const

The reference or binding is to the object in memory and that cannot change because you declared this reference with const. But you can modify the properties of the object in memory. The object itself is mutable.

Any further discussion of bindings is a discussion on Lexical Scope.

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