Siebel Open UI – Useful JavaScript tips

We are moving away from eScript and going towards JavaScript and in future you will be writing more code in JavaScript than in eScript. I believe that eventually Oracle will dump eScript and move to pure JavaScript since it is better, faster and leaner than eScript. It becomes really important that we understand small nuisance and behavior of  JavaScript early in order write better and efficient code. In this post I am sharing few tips/tricks/useful functions that I picked from internet and my experience of working with JavaScript which. I believe these are really important/useful and will come pretty handy while writing Open UI PM/PR.

1. Don’t forget the var keyword when assigning variable’s value for the first time.
Assignment to an undeclared variable automatically results in a global variable being created. Avoid global variables.

2. use === instead of ==
The == (or !=) operator performs an automatic type conversion if needed. The === (or !==) operator will not perform any conversion. It compares the value and the type, which could be considered faster than ==.

'10' == 10     // is true
'10' === 10 // is false

3. Use Semicolon for line termination.Although JavaScript engine automatically inserts ; but using it considered as best practice.

4. Verify if given argument is a number

function isNumber(n){
    return !isNaN(parseFloat(n)) && isFinite(n);
}

5. Get the Max or Min in an array of numbers

var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);

6. Empty an array

var myArray = [12 , 222 , 1000 ];
myArray.length = 0; // myArray will be equal to [].

7. Truncate an array using length

var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].

8. Using Logical AND/OR for conditions

var foo = 10;
foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething();
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();

The logical OR could also be used to set a default value for function argument.

function doSomething(arg1){
    arg1 = arg1 || 10; // arg1 will have 10 as a default value if it’s not already set
}

9. Rounding number to N decimal places

var num =2.443242342;
num = num.toFixed(4);  // num will be equal to 2.4432

toFixed() returns a string not a number

10. Comma Operator

var a = 0;
var b = ( a++, 99 );
console.log(a);  // a will be equal to 1
console.log(b);  // b is equal to 99

11. Using switch/case statement with numeric ranges
You can use numeric ranges in a switch/case statement using below approach

function getCategory(age) {
    var category = "";
    switch (true) {
        case isNaN(age):
            category = "not an age";
            break;
        case (age >= 50):
            category = "Old";
            break;
        case (age <= 20):
            category = "Baby";
            break;
        default:
            category = "Young";
            break;
    };
    return category;
}
getCategory(5);  // will return "Baby"

12. Avoid using try/catch/finally inside a loop
The try-catch-finally construct creates a new variable in the current scope at runtime each time the catch clause is executed where the caught exception object is assigned to a variable.

That’s it for this post, I hope this is helpful. I will be sharing more as and when I find them. Have some of your own tips to share??? Find the comments section.

This Post has been viewed : 7,808 Times


4 Responses to Siebel Open UI – Useful JavaScript tips

  1. foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething();
    foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();

    No, please do not do this !
    this indeed generates the same code, but is far less readable.
    So the if() statement should be prefered in such cases…

    • Since javascript is client side script there are occasions where you want to reduce the size of file and make your code more efficient. These techniques come in handy during those times.
      I agree these things shouldn’t be used when readability is more important than efficiency and size 🙂 (which is the case most of the times).

  2. Does anyone know how to launch Siebel OpenUI in google chrome in debug mode?

    when I try to launch after filling the debug parameters (executabe, cfg, browser and arguments as we used to do in IE8 ) it does not get launched and I get the error message “The Web page was terminated unexpectedly”

    NOTE: I am able to launch in debug mode using firefox and also in Chrome – thick client.

    Not sure if any of you any of you have faced such challenges/issues.

    Any clue/help would be helpful.

    Thanks,
    Sho

Leave a Reply

Contribute