B4J Question [BANano] Creating object with constructor and all attributes/prototype

Toky Olivier

Active Member
Licensed User
Longtime User
Hi,
Can you tell please how to create an Object Constructor like this in full B4X - BANAno code to be used later with another JS Frameworks & Libs:
JavaScript:
function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
}

Person.prototype.name = function() {
  return this.firstName + " " + this.lastName;
};

I can create Person class like this:
B4X:
Sub Class_Globals
    Public firstName As String
    Public lastName As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(first As String, last As String)
    firstName = first
    lastName = last
End Sub

Public Sub fullName() As String
    Return firstName + " " + lastName
End Sub
And I can access this class by this name "banano_myproject_person" in JS but I got this in JS:
JavaScript:
function banano_vkpae_person() {var _B=this;_B.__d6="";_B.__d7="";_B.__d8=0;_B.initialize=function(__176,_last) {_B.__d6=__176;_B.__d7=_last;};_B.fullname=function() {return _B.__d6+" "+_B.__d7;};}
So:
- Properties names are obfuscated so I won't know them
- Constructor are not the same and I cannot write this in Console:
JavaScript:
var person = new banano_vkpae_person("first", "last");

I would like to propose to have:
- An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?)
- Constructor etc will be like here: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Classes/constructor. Transpiled from B4X Initialize subs for example (Initialize, Initialize1, Initialize 2...)
And In B4X, I can get the name of the class easily with something like BANano.GetClassName(Person) where Person is the class name.

Thank you
 
Last edited:
Solution
An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?)
This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true:

B4X:
BANano.TranspilerOptions.DisableShortenVariableNames = True

This will result in BANano not 'obfuscating' the variable names in Release mode.

Constructor etc will be like here: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Classes/constructor. Transpiled from B4X Initialize subs for example (Initialize, Initialize1, Initialize 2...)
The concept 'constructor' does indeed not really exist in B4X and the language works with a different philosophy. The...

alwaysbusy

Expert
Licensed User
Longtime User
An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?)
This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true:

B4X:
BANano.TranspilerOptions.DisableShortenVariableNames = True

This will result in BANano not 'obfuscating' the variable names in Release mode.

Constructor etc will be like here: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Classes/constructor. Transpiled from B4X Initialize subs for example (Initialize, Initialize1, Initialize 2...)
The concept 'constructor' does indeed not really exist in B4X and the language works with a different philosophy. The transpiled Java does internally a simple 'new' constructor, followed by one of the Initialize methods. You have to write several Initialize methods (Initialize, Initialize2, Initialize3,...).

BANano does not transpile to a real JavaScript 'class', but to a 'function'. The reason for that is that BANano supports IE9 (which does not support 'class'). When BANano was first released, IE9 support was still needed by some people. Not sure this is true today and maybe one day it will be converted.

If you want to use a BANano transpiled lib in another JavaScript lib (which is not its main intended use), you will have to follow the same B4X philosophy:

B4X:
var person = new banano_vkpae_person();
person.initialize("first", "last")

And In B4X, I can get the name of the class easily with something like BANano.GetClassName(Person) where Person is the class name.
You can get the transpiled class name with BANano.BN("className") where "className" is a literal string, it cannot be a variable!
B4X:
dim PersonBANanoClassName as String = BANano.BN("Person") ' will return something like "banano_vkpae_person"

Dim P1 As Person = BANano.New(PersonBANanoClassName)
P1.Initialize("first", "last")
Log("I'm " & P1.fullName)

Alwaysbusy
 
Upvote 0
Solution

Toky Olivier

Active Member
Licensed User
Longtime User
Hello @alwaysbusy, thank you for your response. It replies to my questions and requests.
I will see how I can use them. For the class constructor, actually, 93% of browsers have it (https://caniuse.com/mdn-javascript_classes_constructor), so I think it's okay to move but it's up to you. Or if not, having in options: BANano.TranspilerOptions.UseECMAScript2015Class is also an Option.
Just for this:
This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true:

B4X:
BANano.TranspilerOptions.DisableShortenVariableNames = True
Is it possible to have a per class option? I wanted to disable it only for classes used in MVC frameworks, not for widgets/custom views.
It's okay even an simple directive "ignoreshortenvariablenames":
B4X:
Public Sub Proccess_Globals
    Public firstName As String 'ignoreshortenvariablenames
    Public lastName As String 'ignoreshortenvariablenames
End Sub

Public Sub fullName() As String 'ignoreshortenvariablenames
    Return fistName + " " + lastName
End Sub
For all other Variables, Classes Or Subs without the directive, Obfuscations will be activated.

Thank you so much to hear me.
 
Last edited:
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Is it possible to have a per class option?
I see what I can do. Sub names are never Obfuscated. I tried it, but it gave to much trouble as some calls are made with the name of a method as a literal string in some frameworks and this resulted in hard to find bugs.

Alwaysbusy
 
Upvote 0
Top