Boudewijn wrote:While referencing object on forms or the form itself it would be nice ...[stuff]....
this way one could be saved from typing the reference to the form/object on each and every line. Thus a real time saver.
In case this is what you are looking for, this hasn't been made clear yet.
If you are working in a function on form MyForm you do not have to use this syntax
- Code: Select all
forms.MyForm.formVar1 = 'x';
forms.MyForm.formVar2 = 123;
forms.MyForm.foundset.fieldX = 42.13
forms.MyForm.elements.lblInfo = 'Verbosity";
you can just do
- Code: Select all
formVar1 = 'x';
formVar2 = 123;
foundset.fieldX = 42.13
elements.lblInfo = 'Verbosity";
Technically in that example you don't even need the reference to foundset, but we find it's good practice to reference it that way so that it's clear that is where the action is happening.
As far as using with(blah) {} vs assigning what you are working with to a local variable, as far as javascript is concerned there's not much if a difference.
- Code: Select all
var a = elements.lblInfo
All this does is create a reference to elements.lblInfo, it points to the same memory and everything...it's semantically the same...in fact, elements.lblInfo is itself just another variable in javascript that is a reference to a Java object in the underlying framework.
The difference that you get with using with(blah) {} is (if I recall my finer points of javascript correctly) that it creates another node on the scope chain, changing what the
this keyword is looking at. However, like Paul said, this is a language feature that is scheduled to be deprecated in a future version of JavaScript, so unless you want to have to change your code when the Servoy JavaScript engine gets updated, it might not be a great idea to use it.
If you are really looking for that kind of block scoping JavaScript does provide another pattern you can use which looks something like this
- Code: Select all
(function (o)
{
o.property1.value = 1
o.property2.value = 'x'
o.method()
})(objectToWorkWith);
Where you create a one off anonymous function and pass your object in to do all the work on. You get your localized block scope, your shortened typing times, but you still have to reference your object with o.
As you can see, there are several ways to accomplishing the same concept that you are talking about, however whenever you switch languages you should expect to have to endure a few syntactical and paradigm changes.