4 ms·
My background seems to be significantly different from most people here - what you've written there is something I'd like to learn about, but have no idea where
by ColinWright 12y ago
My background seems to be significantly different from most people here - what you've written there is something I'd like to learn about, but have no idea where to start.
I've copied your code, hit "F12" in Firefox, but where do I paste it? Where/how does it run?
How can I learn the basics of this stuff without wading through the mindless tutorials intended for people who have never coded?
For reference, I've written safety-critical real-time embedded systems in assembler and C, and these days I write number-theory and expert systems code in Python.
Where can I start that doesn't patronise me to death?
- teen 12y agoin the console window of the browser tools in chrome or firefox. https://developer.chrome.com/devtools#console https://developer.chrome.com/devtools#console note: javascript always runs in the context of a root object. in the browser, it's the window object. if the webpage has multiple frames, you might have to select the correct frame for the console to execute the js in.
- ColinWright 12y agoThank you - that's got me started - I'll see if I can get more time to explore this later today.
- pbhjpbhj 12y agoI'm a non-programmer [I've only made programs in BASIC, Fortran, C, C++, assembler, smalltalk, javascript, BASH, probably a couple I forgot (does Maple count), and most recently python (lol - I mean to say my level is 'interested amateur')] but I found this [1] quite a nice reintroduction to javascript the other day. It gives a good review of javascript syntax, goes over the intricacies of the loose typing system and variable scope, covers some detail on avoiding memory leaks and some browser specifics (which I think are probably out of date; I didn't review it thoroughly I was playing Minecraft at the time) and gets up to anonymous functions and closures. Aside: Incidentally if someone would like to explain closures to me I'd appreciate it - I don't really understand them. Code Academy's [2] javascript looks very basic but Code School [3] seems to have better beginner stuff - I've done their git/jQuery courses before and they were quite well done. HTH. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re... [2] Avoid for javascript, very basic: http://www.codecademy.com/courses/getting-started-v2/0/1?curriculum_id=506324b3a7dffd00020bf661 http://www.codecademy.com/courses/getting-started-v2/0/1?cur... [3] https://www.codeschool.com/courses/javascript-best-practices https://www.codeschool.com/courses/javascript-best-practices
- ColinWright 12y agoThank you - I'll check them out later.
- tankenmate 12y agoAs an old joke (a ha-ha only serious joke) goes, closures are a poor man's object orientation, and object orientation is a poor man's closure. Going on the list of languages you have above I'll take it from the object orientation side of the argument. Object orientation works by binding functions to data; the data is persistent for this instance of the object (in the sense that the data remains available as long as the object does) and can be manipulated by the functions bound to that data. Closures on the other hand bind data to functions; so the function is always callable but the data is bound to this instance of the function. So you can have many instances of a function each with it's own data bound to it. In most languages that support closures functions are considered a "first class" data type; i.e. individual functions (and the data bound to them) can be passed just like variables (sometimes with caveats depending on the implementation). Typically this comes hand in hand with "anonymous" functions; functions that don't have an entry in the symbol table, i.e. they don't have a name in the function / object namespace. If you truly want to understand how it works then you'd probably want to write a compiler that supports closures; you'd then need to cover issues like how to allocate functions on the heap, the difference between locally allocated (typically on the stack) data bound to functions and globally allocated (typically on the heap) data bound to functions. The second part to closures is understanding the theory, or the why and the high level functionality that it enables, such as callbacks with state or interactions with the likes of recursive (y-combinator like) functions. EDIT: minor grammar fix
- Lawtonfogle 12y agoAm I correct in my understanding that with a closure, it is one or more data bound to one function, while with object orientation, one or more functions are bound to one or more data?
- Jtsummers 12y agoYes, but it can go a bit further. http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent A singular closure is 1+ data bound to an entity (function). A singular object is multiple data/functions bound to one entity (object). In the link above there are examples of either generating multiple closures tied to the same set of data (like tel's example), and also examples of having a singular closure that takes as a first parameter a "method" name and branching based on that. Using either solution you have an OO system using closures as your basis.