Sub Process_Globals
Private jsGen As MiniJS
End Sub
Sub AppStart (Args() As String)
jsGen.Initialize
' Generate a sample function
GenerateSampleFunction
' Get generated code with script tags
Dim jsCode As String = jsGen.Generate
' Or without script tags
Dim jsCodeRaw As String = jsGen.Generate2
Log(jsCode)
End Sub
Private Sub GenerateSampleFunction
jsGen.AddMultiLineComment("Sample generated JavaScript function" & CRLF & "Generated by B4J")
' Start function: function calculateTotal(items, taxRate) {
jsGen.StartFunction("calculateTotal", Array As String("items", "taxRate"))
' let subtotal = 0;
jsGen.DeclareVariable("subtotal", "0", False)
' for (let i = 0; i < items.length; i++) {
jsGen.StartForLoop("let i = 0", "i < items.length", "i++")
' subtotal += items[i].price;
jsGen.AddLine("subtotal += items[i].price;")
' }
jsGen.EndForLoop
' if (taxRate > 0) {
jsGen.StartIf("taxRate > 0")
' const tax = subtotal * taxRate;
jsGen.DeclareVariable("tax", "subtotal * taxRate", True)
' return subtotal + tax;
jsGen.AddLine("return subtotal + tax;")
' } else {
jsGen.AddElse
' return subtotal;
jsGen.AddLine("return subtotal;")
' }
jsGen.EndIf
' }
jsGen.EndFunction
End Sub