这个 jQuery 在做什么?

发布于 2025-01-03 06:11:52 字数 216 浏览 0 评论 0原文

我称自己为中级 jQuery 开发人员,但我对这一行(来自 Twitter 的 Bootstrap)的作用感到困惑:

$tip.find('.help-popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)

具体来说,是方括号之间的部分。有人可以向我解释一下吗?

I would call myself an intermediate jQuery developer, but I'm confused about what this line (from Twitter's Bootstrap) is doing:

$tip.find('.help-popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)

Specifically, the part between the square brackets. Can anybody explain it to me?

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

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

发布评论

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

评论(3

无声无音无过去 2025-01-10 06:11:52
$tip // tip object
.find('.help-popover-title') // find elements of this class

// if the data inside the title variable is an object
// use the append method otherwise use html method
[$.type(title)  == 'object' ? 'append': 'html']  

(title) // lastly, execute the selected function and pass in the title var

内部语句使用三元运算符。它基本上是单行 if..else 语句,

x = 5;

x === 5 ? true : false; // true
x === 4 ? true: false; // false

由于所选方法位于括号内,因此您可以使用字符串来选择方法
它等于:

$tip['append'](title) === $tip.append(title)
$tip // tip object
.find('.help-popover-title') // find elements of this class

// if the data inside the title variable is an object
// use the append method otherwise use html method
[$.type(title)  == 'object' ? 'append': 'html']  

(title) // lastly, execute the selected function and pass in the title var

The inner statement uses a ternary operator. It's basically a single line if..else statement

x = 5;

x === 5 ? true : false; // true
x === 4 ? true: false; // false

Since the selected method is inside the brackets, you can use a string to select a method
It's equalvelent to:

$tip['append'](title) === $tip.append(title)
彼岸花似海 2025-01-10 06:11:52

它是一个内联 if 语句,也称为三元运算符。基本上,如果标题类型是“object”,那么它将获取索引“append”,否则获取索引“html”。希望这就是您问题的意思。

It's an inline if statement, otherwise called a ternary operator. Basically, if the type of title is 'object' then it's getting the index 'append', otherwise the index 'html'. Hope this what you meant by your question.

︶葆Ⅱㄣ 2025-01-10 06:11:52

这里的重要概念是,对象属性不仅可以按字面直接访问,还可以使用包含带有属性名称的字符串(文字或变量)的方括号来访问。此外,函数始终是对象的属性——即使只是全局上下文。

首先,检查基于值的属性:

var myobj = {
   animal: 'frog',
   color: 'blue',
   fly: function() {/*fly*/},
   hide: function() {/*hide*/}
};
alert(myobj.animal); // alerts 'frog';
var prop = 'color';
alert(myobj[prop]); // alerts 'blue';

然后请注意,当属性值是函数时,它不会改变任何内容。它们仍然以相同的方式访问:

myobj.fly() // run function fly
prop = 'hide';
myobj[prop]() // run function named in variable 'prop', which is 'hide';

因此最终,您发布的代码片段只是检查 title 变量的类型并选择适当的函数以使其成为找到的元素的子元素。如果 title 是一个对象,则附加它。如果不是(它必须是文本),则使用 html 函数代替。以这种方式编码是为了节省重复代码或声明新变量。

三元运算符是正常过程 if 语句的表达形式(也就是说,它们计算某些内容而不是控制流程)。下面的示例将说明这一点:

if (a == 1) {return 'yes';} else {return 'no';}
return (a == 1) ? 'yes' : 'no';

VB 的 Iif() 函数和 Excel 的 IF() 工作表函数完全相同。

The big concept here is that object properties can be accessed not just literally and directly but also with square brackets containing a String (literal or variable) with the name of the property. Also, functions are always properties of an object—even if only the global context.

First, check out value-based properties:

var myobj = {
   animal: 'frog',
   color: 'blue',
   fly: function() {/*fly*/},
   hide: function() {/*hide*/}
};
alert(myobj.animal); // alerts 'frog';
var prop = 'color';
alert(myobj[prop]); // alerts 'blue';

Then note that when the property values are functions it doesn't change anything. They are still accessed the same way:

myobj.fly() // run function fly
prop = 'hide';
myobj[prop]() // run function named in variable 'prop', which is 'hide';

So ultimately, the code fragment you posted is just checking the type of the title variable and choosing the appropriate function to make it a child of the found element. If the title is an object, append it. If it's not (it must be text) then use the html function instead. It was coded this way to save duplicating code or declaring a new variable.

Ternary operators are the expressive forms of normal procedural if statements (that is, they evaluate to something rather than controlling flow). The following example will show this:

if (a == 1) {return 'yes';} else {return 'no';}
return (a == 1) ? 'yes' : 'no';

VB's Iif() function and Excel's IF() worksheet function are exactly equivalent.

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