JavaScript foo.prototype.bar notation
Published · tagged with JavaScript
As a follow-up to the post documenting a few popular HTML element + attribute notations, here’s a similar one about JavaScript.
When writing (in text, not in JavaScript) about properties on specific prototypes, you may use the JavaScript notation, foo.prototype.bar. However, this shorter (non-JavaScript) notation is often used instead: foo#bar. Basically, the hash (#) stands for .prototype. — fancy huh?
Let’s see an example:
var numbers = [1, 2, 3]; // array literal
numbers.push(42); // `numbers` is now `[1, 2, 3, 42]`
Here, the Array.prototype.push method is called. You could also say Array#push is called.
A note about jQuery
For convenience, jQuery aliases jQuery.prototype to jQuery.fn. Don’t let this fool you! Whenever you use e.g. jQuery(elem).remove() you’re still calling jQuery.prototype.remove (to which jQuery.fn.remove is a reference), or — using the short notation — jQuery#remove.
Also note the difference between e.g. jQuery#map and jQuery.map:
jQuery(elems).map(fn); // jQuery#map, which is jQuery.prototype.map (or jQuery.fn.map)
jQuery.map(arr, fn); // jQuery.map
Similarly, when using a jQuery plugin that extends jQuery.fn, you’re really extending jQuery.prototype. I’ve been using the short notation when talking about jQuery plugins, e.g. jQuery#placeholder.
IDL attributes
In the spec world, sometimes the Foo#bar notation is used to refer to the bar IDL attribute of Foo rather than foo.prototype.bar.
Leave a comment
Comment on “JavaScriptfoo.prototype.bar notation”
Name *
Email *
Website
Your input will be parsed as Markdown.
Spammer? (Enter ‘no’) *

























