Using Tensorflowjs expect to see a wide variety of types of functions

Click on each of the buttons and then have a look at the example code
See the video here



Traditional Javascript function, updated by using the "async" and "await" key words. Note: Anything within the Script tag cannot be edited from this webpage. Just download to a text editor for the edits to work. See next example for an editable version.



...











Hold the Press!

This is not on the video. Since about August 2018 I have been doing my functions using a slightly different way. This was for various reasons connected to how javascript makes global variables. Using this method the functions become global.

I also have started using the style tag instead of the script tag since a style tag can load scripts dynamically.



...











This function within the button is editable from this online webpage.



...











Very powerful inline function example, This is the form that I prefer since the keyword "function" reminds beginners what is happening with the code.



...











Demonstrating the "arrow" function. This is often used by the Tensorflowjs community. Note the similarities between the inline function and the arrow function. They both work exactly the same, just the Arrow function has less typing and the arrow is after the brackets whereas the inline function key word is before the brackets used for passing variables



...











All three types of functions used together so you can see how they are written.



...











I often see Tensorflowjs code that returns an array of information from the function into an array of variables. Made the code very confusing when I first saw it.



...











Tensorflowjs also often returns an object from a function into several variables. This also made the code very confusing when I first saw it



...











All of the Tensorflowjs examples use modules, which works great for build tools and reducing the number of global variables and can be made with only a few changes to work with pure Javascript, but has CORS issues (Cross-Origin Resource Sharing ) when run from your local machine. (Which is strange since all the files are on the local machine but it is a security issue that is a pain to change on Chrome) My solution to this issue is to use namespacing in the external js files. For namespacing to work you have to change any regular functions into inline or arrow functions. My solution works really well as the code used to make the web page is the exact code that any user sees when they "Right click-View page source" on the page. With build tools the final code is very different from the original Javascript or Typescript



...














This Github, ... this Github Website Version, ... this Hosted Website Version, ... Tensorflowjs

By Jeremy Ellis
Twitter@rocksetta
Website http://rocksetta.com
Use at your own risk!