Mathias Bynens

JavaScript 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, 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(elems).map(fn); // jQuery#map, which is (or, fn); //

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

About me

Hi there! I’m Mathias. I work on Chrome DevTools and the V8 JavaScript engine at Google. HTML, CSS, JavaScript, Unicode, performance, and security get me excited. Follow me on Twitter, Mastodon, and GitHub.


wrote on :

Drew: I don’t take credit for the idea though — this notation has been around for quite a while, and to be honest I don’t know who came up with it. I’m just trying to document it here since a lot of people are confused the first time they see it. I know I once was :)

Kit Cambridge wrote on :

Mathias: I believe Java uses member dot notation (e.g., Array.push) for class and instance methods, though this is clearly problematic in JavaScript for the reasons outlined in your post. I’ve also occasionally seen Ruby’s :: operator used to refer to instance methods (e.g., Array::push).

@nsolsen wrote on :

I like this kind of notation. I have been developing a lot of C++ over the years. In C++ the notation is double colon (foo::bar).

@jayferd wrote on :

I’ve been using Foo::bar, which in CoffeeScript actually expands to It also helps me remember that JS prototypes are quite different from Ruby’s class/module system.

It also gives you a couple of neat extras:

Foo::bar # =>
Foo:: # => Foo.prototype
@:: # => this.prototype (only really useful on Function.prototype)

Leave a comment

Comment on “JavaScript notation”

Your input will be parsed as Markdown.