Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today we're discussing how to work with variables and bindings in the Nashorn JavaScript engine. Bindings allow us to transfer data between Java and JavaScript. Can anyone tell me what they think bindings might mean?
I think it's like linking two things together?
Exactly! It’s like a bridge that connects our Java variables to JavaScript. Remember the acronym 'LINK' to help you recall that Bindings: L for Link, I for Is, N for Necessary, K for Knowledge!
How do we actually create these bindings?
Great question! We create bindings using engine.createBindings() in our code. After that, we can use put() to add our Java variables!
Now that we’ve created our bindings, let’s see how we can use them in our scripts. When we set the bindings with engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE), how do you think it affects the accessibility of our data in JavaScript?
I guess it makes those variables available in the JavaScript context?
Exactly! The variables become accessible to the JavaScript code. Let's take an example where we sum two numbers. If x=10 and y=20, when we run print('Sum = ' + (x + y));, what do you expect the output will be?
It should print 'Sum = 30'!
Exactly! That's how we can dynamically interact with our JavaScript using Java variables.
Bindings can be useful in many scenarios, like controlling the flow of scripts based on Java application's state. Can someone think of a situation we might need to use bindings?
Maybe if we have settings configured in Java that we want to affect how the script executes?
Exactly! For instance, if a user selects settings in a Java application, we could bind those values to a script that makes decisions based on those settings. This leads us to dynamic, configurable applications!
That sounds really powerful!
It is! Remember, the strength of scripting with bindings lies in its flexibility and the ability to modify behaviors on the fly. Alright, let’s recap what we learned today about bindings!
When Java and scripts do meet, Bindings make their message sweet.
Imagine a bridge (Bindings) connecting Java land to JavaScript city, facilitating the flow of data across the river of execution.
B.I.N.D. - Bindings In Java create Necessary Data flow.
B.A.R.S. - Bindings Allow Real-time Scripting.
Term
What is Bindings?
Click to flip
Term
How do you create a Bindings object?
Click to flip
Term
What scope is usually used for bindings?
Click to flip
Term
What is the significance of using Bindings?
Click to flip
This section discusses how to pass variables from Java to scripts using Bindings in Java's scripting API.
It highlights the use of the Bindings object in the Nashorn JavaScript engine to facilitate communication between Java and JavaScript. The section also provides examples demonstrating how to pass variables and utilize them in scripts.
In the Java Scripting API, particularly with the Nashorn JavaScript engine, developers can seamlessly pass variables from Java to embedded scripts using a special object known as Bindings. Bindings functions like a map, allowing developers to define key-value pairs which can be accessed by the script being executed.
Here's how it works: First, create an instance of Bindings using the createBindings() method of the ScriptEngine. Then, values can be put into the bindings using the put() method to define the variables needed by the script. Once the Bindings are set up with the required variables, developers can specify the scope in which these bindings should be available (commonly the ScriptContext.ENGINE_SCOPE), ensuring the script can retrieve these external values.
For example, if x and y are integers in Java, they can be put into the bindings object. In the script, you can easily sum these numbers and display the result. This capability is crucial for dynamically integrating values from Java into the JavaScript context and enhancing the interactivity of applications.