JavaScript foo.prototype.bar 符号
作为记录一些流行的 HTML 元素 + 属性符号的帖子的后续,这里有一个关于 JavaScript 的类似帖子。
当写(在文本中,而不是在 JavaScript 中)关于特定原型的属性时,我们可以使用 JavaScript 符号 foo.prototype.bar
。 但是,通常使用这种更短的(非 JavaScript)表示法:foo#bar
。 基本上,哈希 (#
) 代表 .prototype
。 - 花哨吧?
让我们看一个例子:
var numbers = [1, 2, 3]; // array literal
numbers.push(42); // `numbers` is now `[1, 2, 3, 42]`
这里调用了 Array.prototype.push
方法。 你也可以说 Array#push
被调用了。
关于 jQuery 的注释
为方便起见,jQuery 将 jQuery.prototype
别名为 jQuery.fn
。 不要让这个愚弄你! 每当我们使用例如 jQuery(elem).remove()
你仍然在调用 jQuery.prototype.remove
(jQuery.fn.remove
是对它的引用),或者——使用缩写符号——jQuery#remove
。
还要注意例如之间的区别。 jQuery#map
和 jQuery.map
:
jQuery(elems).map(fn); // jQuery#map, which is jQuery.prototype.map (or jQuery.fn.map)
jQuery.map(arr, fn); // jQuery.map
同样,当使用扩展 jQuery.fn
的 jQuery 插件时,我们实际上是在扩展 jQuery.prototype
。 在谈论 jQuery 插件时,我一直在使用缩写符号,例如 jQuery#placeholder
。
IDL 属性
在规范世界中,有时 Foo#bar
表示法用于指代 Foo 的 bar IDL 属性,而不是 foo.prototype.bar
。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。