views:

308

answers:

2

Hi, a quick, probably easy question whose answer is probably "best practice"

I'm following a tutorial for a custom-template mobile Safari webapp, and to change views around this code is used:

function btnSave_ClickHandler(event)
{
    var views = document.getElementById('stackLayout');
    var front = document.getElementById('mainScreen');
    if (views && views.object && front) {
        views.object.setCurrentView(front, true);
    }
}

My question is just about the if conditional statement. What is this triplet saying, and why do each of those things need to be verified before the view can be changed? Does views.object just test to see if the views variable responds to the object method? Why is this important?

EDIT - This is/was the main point of this question, and it regards not Javascript as a language and how if loops work, but rather WHY these 3 things specifically need to be checked:

Under what scenarios might views and front not exist?

I don't typically write my code so redundantly. If the name of my MySQL table isn't changing, I'll just say UPDATE 'mytable' WHERE... instead of the much more verbose (and in my view, redundant)

$mytable = "TheSQLTableName";
if ($mytable == an actual table && $mytable exists && entries can be updated){
    UPDATE $mytable;
}

Whereas if the table's name (or in the JS example, the view's names) ARE NOT "hard coded" but are instead a user input or otherwise mutable, I might right my code as the DashCode example has it. So tell me, can these values "go wrong" anyhow?

Thanks!

+1  A: 

The if is testing those 3 pointers to make sure they are non-null. A null pointer is 0 which converts to false. If any of those 3 pointer are 0 (null) then it won't try to use them.

I'm not sure what dereferencing a null pointer does in Javascript but it's an error and may cause an exception. The if is just avoiding that possibility.

progrmr
A: 

Hi I would like to know the "pros and cons of Dashcode and how is it better for development for Web app rather than plain simple HTML or WebKit ObjC API"

Regards' Mreetyunjaay

Mreetyunjaya