Add More Types

Our ‘rgbToHex’ function is still declared with the keyword ‘function’. This is the JavaScript way of declaring a function, and it still leaves our function unsafe.

In JS++, it is best practice to always declare data types when possible. Since we always return a string value for our ‘rgbToHex’ function, we should restrict the return type to ‘string’.

external alert;

string rgbToHex(byte red, byte green, byte blue) {
var r = red.toString(16);
var g = green.toString(16);
var b = blue.toString(16);

while (r.length < 2) r = "0" + r;
while (g.length < 2) g = "0" + g;
while (b.length < 2) b = "0" + b;

return "#" + r + g + b;
}

alert(rgbToHex(255, 255, 255));

Now all ‘return’ statements inside the function must return an expression that evaluates to string data.

Last but not least, look at these statements:

var r = red.toString(16);
var g = green.toString(16);
var b = blue.toString(16);

What methods are being called?

Remember, a lot of programming with types in JavaScript is based on intuition. (The JS++ type system builds on top of this intuition so programming in JS++ should feel like a natural progression for JavaScript developers.) In each of the above statements, ‘toString(16)’ is being called. In other words, we know we should expect string data. Let’s change our ‘var’ to ‘string’ then:

external alert;

string rgbToHex(byte red, byte green, byte blue) {
string r = red.toString(16);
string g = green.toString(16);
string b = blue.toString(16);

while (r.length < 2) r = "0" + r;
while (g.length < 2) g = "0" + g;
while (b.length < 2) b = "0" + b;

return "#" + r + g + b;
}

alert(rgbToHex(255, 255, 255));

Compile main.jspp. Delete the rgbtohex.js JavaScript file. Edit index.html to completely remove the reference to the JavaScript rgbtohex.js file. Your index.html code should now look like this:

<!DOCTYPE html>
<head>
<title>RGB to Hex Conversion</title>
</head>
<body>
<script src="main.jspp.js"></script>
</body>
</html>

Open index.html in your web browser. You should see a message box with “#ffffff”. Congratulations! You’ve just changed a JavaScript function into JS++.

JS++ | Type System

Similar Reads

RGBtoHex: Using JavaScript from JS++

We’re going to create two files, a JavaScript file and a JS++ file, to introduce how they interact together. We’re going to use the classic ‘rgbToHex’ example to understand JS++ type guarantees....

Type Guarantees

“Type guarantees” are a unique feature of JS++ that mean your types are guaranteed to be correct during compile-time analysis and runtime code execution even when you’re polluting your JS++ code with untyped JavaScript code. It’s the closest thing you’ll find to the type safety featured in safer languages that don’t have to deal with large collections of untyped code....

RGB Values

The RGB color model defines three color values: red, blue, and green. These color values are numeric and must fit within the range 0-255. JS++ actually happens to have a data type that exactly fits the specification of being numeric and guaranteeing numbers to be in the 0-255 range: the ‘byte’ data type....

Examining the Untyped JavaScript

RGB values must be within the 0 to 255 range. Since JavaScript lacks data types, we have to manually perform these checks. Let’s inspect how we handle this in our ‘rgbToHex’ JavaScript function....

Using JavaScript More Safely

You can use JavaScript more safely from JS++ than you can from JavaScript itself....

Move rgbToHex to JS++

First, let’s stop importing the JavaScript ‘rgbToHex’ function by removing the ‘external’ statement that imports it. Our main.jspp file should now just look like this:...

Converting the “Untyped” JavaScript into “Typed” JS++

We’ll start converting our JavaScript ‘rgbToHex’ function into JS++ by removing all the runtime checks and errors. You won’t be needing them anymore....

Add More Types

Our ‘rgbToHex’ function is still declared with the keyword ‘function’. This is the JavaScript way of declaring a function, and it still leaves our function unsafe....

Other Considerations

As we learned in the previous chapter about JavaScript, we intuitively have a sense of the data type to expect....

Final Note

Why are JS++ type guarantees necessary? Because, with unsafe JavaScript, even large websites with resources, like GoDaddy, can have broken checkouts resulting in lost sales from a single TypeError:...