Show HN: jQuery.pin – a plugin for making stuff stick
webpop.github.com
webpop.github.com
Resig wrote about this after it horribly mauled Twitter a few years back: http://ejohn.org/blog/learning-from-twitter/
This plugin looks well-done, but I'm looking forward to a day when we don't need JavaScript to control layout like this.
We first developed the plugin for the Webpop gallery page (http://www.webpop.com/gallery) to make the description next to each screenshot stick to the top while you were viewing that gallery entry.
Later we also used it for the pinned navigation in our documentation section to make sure it didn't stay fixed and overlapped the footer once you scroll down to the bottom of the page. As far as I know Bootstrap's affix doesn't really help you there.
- iPad 4 running iOS 6.0 (10A403) with dontCheckForPositionFixedSupport: true
- iPad 4 running iOS 6.0.1 (10A523)
- iPad 1 running iOS 5.1.1 (9B206) with dontCheckForPositionFixedSupport: true
- Nexus 7 running Android 4.2.1 with dontCheckForPositionFixedSupport: true
Unfortunately, I am mobile challenged so I cannot test these out for myself.
I was thinking about posting it on HN when I got a break in the action. Haven't had a break in the action yet.
Also, what is with this new fashion of declaring functions as anonymous methods?
var recalculateLimits = function () {
/* snip */
};
var update = function () { recalculateLimits(); onScroll(); };
Have I missed a memo?EDIT: I forgot to say apart from my moaning, does the job, good code, good job! And it's only jerky when you use the scroll button on a mouse.
I just don't get what it adds to your code apart from hiding simple function definitions in the visual trappings of a static helper object or closure. Javascript is already homogeneous enough without making it visually even more.
What method do you use instead of the following, when creating namespaces?
var yourNamespace = {
foo: function() {
},
bar: function() {
}
};Did you messed up the wording over homogeneity?
From the other commenter's SO link, I found this link to go more in depth into the differences of declaring functions, and it seems to come down to having stricter control on the order of events with no surprises.
http://javascriptweblog.wordpress.com/2010/07/06/function-de...
And here's an example of one of the surprises you'll run into if you don't declare your functions in this way: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoistin...
Your second link describes both variable hoisting, a real problem which causes serious problems, and function hoisting that has never been a problem as almost every other modern language behaves that way.
You don't seem to be a polyglot or you'd understand the problem immediately. Variable hoisting is just downright bizarre if you've worked in almost any other language, whereas function declarations can be anywhere in most languages and so when they are declared is almost completely irrelevant to a programmer.
Artificially assigning them to a variable so that suddenly function declaration order does matter is just downright weird. The 'normal' function declaration is better and clearer.
To your point about this not being in the book JavaScript the Good Parts, I re-downloaded my purchase from Oreilly to double check, and throughout the book, all functions are declared as variables. Here's a screenshot from the second page of the "Functions" chapter, which is the first time a function declaration is introduced in the book: https://dl.dropbox.com/u/1517499/Screen%20Shot%202013-03-26%...
Edit: Just an edit to add that I quickly perused the pdf and couldn't pick out anywhere where he explained why he recommends this way. In any case, I'm always open to any arguments one way or the other.
It's this:
var foo = function() { redeclaration(); }
that I believe he's referring to, unless I'm mistaken.
http://javascriptweblog.wordpress.com/2010/07/06/function-de...
From that link:
"b) Function Expressions are more versatile. A Function Declaration can only exist as a “statement” in isolation. All it can do is create an object variable parented by its current scope. In contrast a Function Expression (by definition) is part of a larger construct. If you want to create an anonymous function or assign a function to a prototype or as a property of some other object you need a Function Expression. Whenever you create a new function using a high order application such as curry or compose you are using a Function Expression. Function Expressions and Functional Programming are inseparable."
Also this link: http://kangax.github.com/nfe/
brings up these disadvantages to function declarations:
* can't conditionally declare functions with consistent results across browsers * function declarations cannot appear in blocks (technically), though many implementations allow this in practice
finally, i'd argue that function expressions encourage you to think of functions as the first class objects js intended them to be
Crockford himself seems to have abandoned the habit if you check his github code.
What you suggest makes no sense in Javascript. Variable functions allow the developer to simulate namespacing in JS without polluting the global namespace.
But about the hoisting problems... For me, the real problem is that you're declaring a function inside a function. Or using variables with the same name. That's a bad practice, it doesn't make sense. It should be avoided instead of using language constructs to work around it.
Named function expressions demystified http://kangax.github.com/nfe/
But at no point do the use this technique for declaring functions any where else.
Interestingly in that article they don't even entertain the possibility that someone would ever declare a javascript function that way. A mere 3 years ago.