简体   繁体   English

javascript简写语法

[英]javascript shorthand syntax

I was wondering how this could be written in shorthand that the statement would execute 我想知道如何将其写为该语句将被执行的简写形式

This one isn't working, but I see this kind of syntax lots of times in plugins - variables mixed with statements etc.. 这是行不通的,但是我在插件中经常看到这种语法-变量与语句等混合。

Can someone give an explanation regarding the proper use of this shorthand syntax? 有人可以给出有关此速记语法正确用法的解释吗? I want to "execute" NOT "evaluate" the second statement if the first evaluates to true! 如果第一个语句为true,我想“执行”而不是“评估”第二个语句!

var succes = !data.user||(window.location = "users/profile");

I knew the first example was way to simple, This one is better, it also uses comma,s to string statements after eachother, I like to know how to learn this syntax. 我知道第一个示例是简单的方法,这是更好的方法,它也使用逗号将彼此后面的语句串起来,我想知道如何学习这种语法。

},
        hide: function (a,
        b) {
            if (f && !(500 > (new Date).getTime() - f.getTime())) {
                if (!a || "number" == typeof a) a = k();
                b || (b = $(".profile-popup"));
                j(a) && (b.fadeOut("fast"), m(!1, a));
                e && (clearInterval(e), e = null)
            }
        }
    }
}();

EDIT I changed my first example to use the && in my code and it worked, so, that's that - for anyone else reading -, and you should use absolute url's if working with window.location 编辑我更改了我的第一个示例,在我的代码中使用了&&,并且它起作用了,就是这样-对于其他阅读的人-如果使用window.location,则应该使用绝对网址

I also found another detailed explanation over here . 我还在这里找到了另一个详细的解释。

thanks, Richard 谢谢,理查德

I think you're missing another = : 我认为您缺少另一个=

var succes = !data.user || (window.location == "users/profile");

Your example assigns, whereas == is a comparison. 您的示例分配,而==是一个比较。

The general pattern of !obj || obj = "something" !obj || obj = "something"的一般模式 !obj || obj = "something" is simply shorthand for: !obj || obj = "something"简写为:

if (obj === undefined) {
    obj = "something";
}

That's because !obj evaluates to false if it's undefined (the pattern also seems to assume that obj will not be defined as true ). 这是因为!obj如果未定义,则求值为false (该模式似乎还假定 obj不会定义为true )。

Likewise, the pattern f(a) && (g(b), h(c)) is shorthand for: 同样,模式f(a) && (g(b), h(c))是以下形式的简写:

if (f(a) == true) {
    g(b);
    h(c);
}

For the referenced piece of code: 对于参考的代码:

var succes = !data.user||(window.location = "users/profile");

What this implicitly says is: 这暗含的是:

  1. If data.user is undefined, then set success to true. 如果未定义data.user,则将success设置为true。
  2. Otherwise (if data.user is assigned), then redirect to users/profile . 否则(如果分配data.user ),然后重定向users / profile

The exact meaning is anyone's guess without knowing the context, but it appears to mean "redirect the profile screen, if user data is available, otherwise ..." 确切的含义是任何人都不知道上下文所进行的猜测,但是它的意思是“如果有可用的用户数据,则重定向个人资料屏幕,否则...”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM