暧昧的称呼? JavaScript 函数和对象

发布于 2024-10-27 15:26:12 字数 253 浏览 3 评论 0原文

我在 jQuery 中看到具体

var a= 'something' || function () {

}

var a = 'something' || { }

什么意思?我知道 { } 是 javascript 中的一个对象 json,并且知道 javascript 中的匿名函数,但仍然无法弄清楚这意味着什么。

感谢您提前的帮助。

I see that in jQuery to be specific

var a= 'something' || function () {

}

or

var a = 'something' || { }

What does it mean? I know { } is an object json in javascript and am aware of anonymous functions in javascript but still cant figure out what does this means.

Thanks for the help in advance.

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

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

发布评论

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

评论(4

三生殊途 2024-11-03 15:26:12

您很可能提供了一个错误的示例。您的代码有效,但编写起来毫无用处,因为您将已知字符串分配给变量,这使得以下OR语句无用。

常见的是这样的语法:

function foo(bar) {
   // set baz to the contents of bar
   // or create an empty object if bar evaluates to false
    var baz = bar || {}; 
}

You most likely included a faulty example. Your code is valid, but would be pretty useless to write, since you are assigning a known string to a variable, which renders the following OR statement useless.

What is common, is syntax like this:

function foo(bar) {
   // set baz to the contents of bar
   // or create an empty object if bar evaluates to false
    var baz = bar || {}; 
}
伴我老 2024-11-03 15:26:12

这种“条件赋值”是语言中的常见习惯用法,它利用布尔运算符的短路(如本例中的“OR”)将左侧表达式的值分配给变量(如果该变量的计算结果为“true”值或右侧表达式(如果不是)。考虑一下:

var nullOrFive = null || 5; // => 5

左侧 (null || Five) 计算从左到右测试 OR 运算符的每个操作数,直到找到“真实”的操作数并将其返回。另一方面:

var tenOrWhatever = 10 || someMethodThatIsNeverCalled(); // => 10

因此,在您的示例中,如果“something”的计算结果为 true(除了 JavaScript 中的“undefined”、“null”和零),那么变量“a”将获得其值,否则它将获得函数 (function() {...}) 或对象字面量 ({})。

This sort of "conditional assignment" is a common idiom in languages which takes advantage of short-circuiting of boolean operators (like "OR" in this case) to assign to a variable the value of the left-hand expression if it evaluates to a "true" value or the right-hand expression if not. Consider:

var nullOrFive = null || 5; // => 5

The left-hand side (null || five) evaluates left-to-right testing each operand to the OR operator until it finds a "truthful" one, returning it. On the other hand:

var tenOrWhatever = 10 || someMethodThatIsNeverCalled(); // => 10

So in your examples, if "something" evaluates to true (which is anything but "undefined", "null", and zero in JavaScript), then the variable "a" will get its value, otherwise it gets the function (function() {...}) or object literal ({}).

掐死时间 2024-11-03 15:26:12

当给变量赋值时,JavaScript 会计算给定的表达式。

var s= s || {};

这意味着如果当前作用域中已经存在名为 s 的变量,则新创建的 s 变量将指向它。否则,如果 s 变量未在当前作用域中定义,或者它指向空引用,或者指向计算为 FALSE 的其他值,则新创建的 s 变量将指向一个新对象。当在多个文件中扩展一个对象时,这很有用:

File1

var globalNamespace = globalNamespace  || {};
globalNamespace.someVariable  = "some value;"

File2

var globalNamespace = globalNamespace  || {};
globalNamespace.someFunction = function()
{
    return this.someVariable;
};

通过这种方式,人们可以扩展 globalNamespace 对象,而不必担心代码会被分成多个文件文件。

when assigning a value to a variable, JavaScript is evaluating the given expression.

var s= s || {};

this means that if there already is a variable named s in the current scope, the newly created s variable is pointing to it. Else, if s variable is not defined in the current scope, or it points to a null reference, or to some other value evaluated to FALSE, the newly created s variable will point to a new object. This is useful when one is extending one object in multiple files:

File1

var globalNamespace = globalNamespace  || {};
globalNamespace.someVariable  = "some value;"

File2

var globalNamespace = globalNamespace  || {};
globalNamespace.someFunction = function()
{
    return this.someVariable;
};

In this way, one can extend the globalNamespace object without having to worry that the code is spitted in multiple files.

伴梦长久 2024-11-03 15:26:12

引用MDC上的文档

Logical OR (||)
expr1 || expr2
Returns expr1 if it can be converted to true; otherwise, returns expr2.
Thus, when used with Boolean values, || returns true if either operand is true;
if both are false, returns false.

此外,还有一个短路评估:
由于逻辑表达式是从左到右计算的,因此使用以下规则测试它们是否可能存在“短路”计算,

因此如果 expr1 可以计算为 true,则不会计算 expr2。

jQuery 使用大量的短路评估来定义变量的默认值。例如。
var o = 选项 || {};
将把选项放入变量 o 中;但如果选项未定义或为 null,则将确保 o 初始化为 {}

quoting the documentation on MDC,

Logical OR (||)
expr1 || expr2
Returns expr1 if it can be converted to true; otherwise, returns expr2.
Thus, when used with Boolean values, || returns true if either operand is true;
if both are false, returns false.

Additionaly, there is a short-circuit evaluation :
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules

so if expr1 can be evaluated to true, expr2 is not evaluated.

jQuery uses a lot of those short-circuit evaluation to define default values for variables. eg.
var o = options || {};
will put the options in variable o ; but will make sure o is initialized to {} if options is undefined or null

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