Toky Olivier Active Member Licensed User Longtime User Apr 5, 2022 #1 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: Copy 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: Copy 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: Copy 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: Copy 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: Apr 6, 2022
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: Copy 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: Copy 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: Copy 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: Copy 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
Solution alwaysbusy Apr 6, 2022 Toky Olivier said: An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?) Click to expand... This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true: Code: Copy BANano.TranspilerOptions.DisableShortenVariableNames = True This will result in BANano not 'obfuscating' the variable names in Release mode. Toky Olivier said: 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...) Click to expand... The concept 'constructor' does indeed not really exist in B4X and the language works with a different philosophy. The...
Toky Olivier said: An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?) Click to expand... This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true: Code: Copy BANano.TranspilerOptions.DisableShortenVariableNames = True This will result in BANano not 'obfuscating' the variable names in Release mode. Toky Olivier said: 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...) Click to expand... 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 Apr 6, 2022 #2 Toky Olivier said: An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?) Click to expand... This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true: Code: Copy BANano.TranspilerOptions.DisableShortenVariableNames = True This will result in BANano not 'obfuscating' the variable names in Release mode. Toky Olivier said: 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...) Click to expand... 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: Code: Copy var person = new banano_vkpae_person(); person.initialize("first", "last") Toky Olivier said: And In B4X, I can get the name of the class easily with something like BANano.GetClassName(Person) where Person is the class name. Click to expand... You can get the transpiled class name with BANano.BN("className") where "className" is a literal string, it cannot be a variable! Code: Copy 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 said: An another Module template to create Javascript class where attributes are not obfuscated (or there is 'ingoreobfuscation BANano directive?) Click to expand... This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true: Code: Copy BANano.TranspilerOptions.DisableShortenVariableNames = True This will result in BANano not 'obfuscating' the variable names in Release mode. Toky Olivier said: 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...) Click to expand... 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: Code: Copy var person = new banano_vkpae_person(); person.initialize("first", "last") Toky Olivier said: And In B4X, I can get the name of the class easily with something like BANano.GetClassName(Person) where Person is the class name. Click to expand... You can get the transpiled class name with BANano.BN("className") where "className" is a literal string, it cannot be a variable! Code: Copy 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
Toky Olivier Active Member Licensed User Longtime User Apr 6, 2022 #3 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: alwaysbusy said: This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true: B4X: Copy BANano.TranspilerOptions.DisableShortenVariableNames = True Click to expand... 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: Copy 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: Apr 6, 2022 Upvote 0
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: alwaysbusy said: This can be done by setting the Transpiler option 'DisableShortenVariableNames' to true: B4X: Copy BANano.TranspilerOptions.DisableShortenVariableNames = True Click to expand... 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: Copy 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.
alwaysbusy Expert Licensed User Longtime User Apr 6, 2022 #4 Toky Olivier said: Is it possible to have a per class option? Click to expand... 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
Toky Olivier said: Is it possible to have a per class option? Click to expand... 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