Dashcode 代码翻译

发布于 2024-08-22 02:39:57 字数 1092 浏览 3 评论 0原文

一个快速、可能简单的问题,其答案可能是“最佳实践”

我正在遵循自定义模板移动 Safari Web 应用程序的教程,并使用此代码更改视图:

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

我的问题只是关于 if< /code> 条件语句。这三元组说了什么?为什么在改变视图之前需要验证每一件事? views.object 是否只是测试views 变量是否响应对象方法?为什么这很重要?

编辑 - 这是/曾经是这个问题的要点,它不认为Javascript是一种语言以及if循环如何工作,而是为什么这三件事特别需要进行检查:

在什么情况下viewsfront可能不存在?

我通常不会如此冗余地编写代码。如果我的 MySQL 表的名称没有更改,我只会说 UPDATE 'mytable' WHERE... 而不是更详细的内容(在我看来,这是多余的)

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

。名称(或在 JS 示例中,视图的名称)不是“硬编码”,而是用户输入或其他可变的,我可能会像 DashCode 示例那样纠正我的代码。那么请告诉我,这些值无论如何都会“出错”吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-08-29 02:39:57

if 正在测试这 3 个指针以确保它们不为空。空指针是 0,它会转换为 false。如果这 3 个指针中的任何一个为 0(空),那么它不会尝试使用它们。

我不确定在 Javascript 中取消引用空指针会做什么,但这是一个错误,可能会导致异常。 if 只是避免这种可能性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文