Hacker News new | comments | ask | show | jobs | submit login
Learning Advanced JavaScript (2008) (ejohn.org)
192 points by kparaju on Dec 11, 2016 | hide | past | web | favorite | 55 comments



Once again I am going to shamelessly plug my favorite course author Anthony Alicea and his JS/NodeJS/Angular1 course[1],[2],[3]. I really really liked Tony's course as it gave quite a deep look into topics, which are usually only attainable from books, while at the same time keeping it very interesting and also contain lots of best practice material and being extremely dense but easy to digest. Do let me know if you guys know any other courses of the same style. For quite a while I have been trying to find similar courses/tutorials of similar caliber when it comes to knowledge gained and being easy to digest. If you haven't watched any of Tony's courses, do give them a shot. Rigorous -- but not boring or too long.

[1]https://www.youtube.com/watch?v=Bv_5Zv5c-Ts

[2]https://www.youtube.com/watch?v=ejBkOjEG6F0

[3]https://www.udemy.com/understand-nodejs/?couponCode=LEARNNOD....

Disclaimer: Because it may sound like it, I'd like to clarify I have absolutely no affiliation with Tony or his courses. I just really really liked them.


JS course is fantastic, but the Angular one isn't that great.

Alicea has React course called 'React and Flux for Angular Developers'in Pluralsight if anyone is looking more of his courses.


When I started out, this and "Secrets of the JavaScript Ninja" didn't help me understand. For me, after months of trying to understand JavaScript, Dmitry Soshnikov's post, JavaScript. The Core, made understanding closure, prototype, and scope chain crystal clear in about 30 minutes. [0] Also, every time someone shares a basics on JavaScript link on Hacker News I also share my interview guide which despite its popularity has yet to land me a job writing JavaScript. Nonetheless, I love writing JavaScript and understanding it and some of the APIs like Angular, Lodash, Nightmare, and async makes me feel empowered. It doesn't cover any topics in ES6 yet.[1]

[0] http://dmitrysoshnikov.com/ecmascript/javascript-the-core/

[1] https://github.com/adam-s/js-interview-review


This is fairly old. It goes with John Resig's book "Secrets of the JavaScript Ninja Released" which was released at the beginning of 2013 [1]. I remember seeing this site up well before that. He started the book in 2008.

If you can work your way through this tutorial you'll gain a decent grasp of ES5 prototypes and functions.

[1]: http://ejohn.org/blog/secrets-of-the-javascript-ninja-releas...


The second edition of that book was released this year.

https://www.amazon.ca/Secrets-JavaScript-Ninja-John-Resig/dp...


He's not wrong. Yeah I knew I saw this somewhere before.


Since we are talking about learning Javascript, there is a very interesting approach in this edX course: Teach biology and Javascript!

"Nature, in Code: Biology in JavaScript" -- Learn JavaScript programming by implementing key biology concepts in code, including natural selection, genetics and epidemics.

  Instead of just learning programming principles outside of
  any context, you will learn JavaScript programming by
  implementing key biological concepts in code so they can
  run in your browser.
https://www.edx.org/course/nature-code-biology-javascript-ep...


Haven't gone past the first slide yet, but FWIW with ES6 you could do something like:

  [nikki ~]$ node
  > const bind = (fn, obj, ...args1) => (...args2) => fn.call(obj, ...args1, ...args2)
  undefined
  > bind(console.log, console, 'a', 'b')('c', 'd')
  a b c d
  undefined
  > // calls `fn` with `obj` as `this` and the remaining arguments prepended with given ones
I believe all of the old-style argument stuff with `arguments` is possible with `...`, not sure though, maybe some edge cases.


This seems like very beginner level JavaScript. Defining Functions, Functions as Objects, Context? All JS fundamental concepts.


Because of JavaScript's prototypical nature, there are some nuances of the language that many people that have been using JavaScript for a while might not have known.

Things like new on functions, context, how prototypes work, etc...

Now with ES6/7/Typescript coming along, you're going to have a new generation of JS programmers that won't even realize you can "new up" a function.


I think now it's all considered basic since JS ecosystem has grown a lot and people take it for granted, but probably back then these were all advanced materials you couldn't find easily.


No, they were still basic in 2008. (ES3 was published in 1999.) JavaScript hasn’t changed very much as a language; just the tools around, mostly.


In 2008 javascript programmers were called "script kiddies" and the language was only starting to get wider adoption. Unless you were already an experienced web programmer back then this was advanced material for most web programmers who only used to use javascript for displaying alerts and cute dynamic features.

Speaking from my own experience, I didn't know about all these prototypes and important concepts for a long time and and did fine. I built tons of web apps without knowing these "advanced" features. It was only when I decided to learn the language seriously that I got acquainted with a lot of these features like the prototype concept, object constructors, etc.

If you can build pretty advanced production websites without knowing these features I would say these features are advanced. That said, it's a different story now because javascript is an important language. But my point is in 2008 it wasn't.

Also this guy created jQuery, if he says it's advanced, it probably was.


> In 2008 javascript programmers were called "script kiddies"

Yeah, by pretentious people who thought…

> most web programmers who only used to use javascript for displaying alerts and cute dynamic features.

… that. I wouldn’t call someone who only knows how to display an alert a “JavaScript programmer” or base what are considered advanced concepts off of them.

> Also this guy created jQuery, if he says it's advanced, it probably was.

jQuery is a pretty normal library that gained a lot of popularity. It’s not particularly advanced.


Javascript hadn't changed much up until the last few years.

Now with ES6 (ES2015), ES2016, ES2017, etc... It's beginning to add a not-insignificant amount of features to the core language which at least to me has completely changed how I write it (for the better)


I found that very valuable, even years into JS. Not in this particular book, but, Javascript Allongé has a similar approach. It gave me a fresh perspective on the language and functional programming (in JS), as well as re-explain the basics (since you can't have the basics explained enough)


Could you point to more advanced concepts? As a newb, I'm eager to learn.


If you want to be a good programmer, pick another language, and start there. you will learn more good practices from starting with a modern, full-featured language like python, than trying to work in the the chaotic disaster zone that is javascript.

A good programmer can pick up javascript quickly. Other than syntactic and semantic warts, it is not a difficult language to learn.


Thanks for the suggestion.

I actually started with Python and moved on to JavaScript for my work involving web development. Currently, I use both.

I'm generally comfortable with making CRUD apps employing OOP, using both Python classes and prototypal inheritance in JS. My issue is that I still don't have the experience to understand WHY people often lament the 'chaotic disaster' that is JS. I haven't had any issues yet...

I also just want to keep the momentum up and never stop learning.


JavaScript just isn't the "chaotic disaster" many try to paint it as anymore. Sure, you can write it in a chaotic way, but the level of understanding and education has helped make it easier for people coming into JavaScript actually do well, and avoid the major stumbles that caused issues in the early days. Add in advances to the language itself over the lat few years, and it's a very solid language.


I don't really want to stoke a language war, but if it truly isn't immediately obvious to you why a language like Python is less of a mess than JS (which lacks fundamental things like "packages"), then you aren't done with Python yet.

I don't mean that to be rude. It's just...the bar is very low here.



you mean, the future version of javascript that nobody uses yet has modules:

https://developer.mozilla.org/en/docs/Web/JavaScript/Referen...

meanwhile, everyone else uses one of the sixteen competing implementations of something that was built-in to any real language starting in the 70s. that's not a mess at all.


Seems like a typical response by a non-JS programmer that doesn't really understand where JavaScript is at these days.


I second this. It was so easy to write JavaScript after writing C# and others for awhile.

Now I write mostly JavaScript (<3 TypeScript) because it's so easy and fast, but having traditional OOP principals in my head is very helpful.

You'll also find many more, much better examples for data structures and algorithms in Java or C#.


Java and C# aren't going to give you the same flavour of protypical inheritance that JavaScript has.

Perhaps you might try Lisp or scheme:

http://www.crockford.com/javascript/little.html


Neither Lisp nor Scheme offer prototypical inheritance out of the box. Lisp uses ol'-fashioned nominal subclassing, while Scheme doesn't even have "objects" in the OOP sense. They're powerful enough to add it, but nobody does because the prototypical object model pales in comparison to CLOS. Come to think of it, every object model that's not CLOS pales in comparison to CLOS...

I think this is a poor recommendation regardless, because programming in JS really is more similar to modern C# than it is to Lisp or Scheme.

Also, Douglas Crockford is an asshat. How did he wind up an authority on anything?


No, not the same. The general concepts are similar, though.

My thinking goes: understanding classic OOP is helpful in learning JavaScript because the high level concepts are the same but in my opinion more obvious and easier to implement in Java/C#.

By more obvious, a Java/C# (+ others) program starts with a class and to add functionality you either have to add more methods to that class or implement another class. So to a new programmer "duh, everything is an object and objects are instances of a class".

It's kind of hard to convey the same idea to someone in JavaScript when {} is called an object, "xxx" is an object, and to compose a new class, you compose a new function.

However, to someone who already understands classes and objects, it's just a new/different way to create classes and objects.


Try not using prototype or class in JavaScript for a while, and you will do just fine without them. If there's a paradigm, or best practice, and you want to learn, don't follow it and see what happens.


I write procedural scripts all the time for just getting things done, often in JavaScript, Perl and PHP.

I've only worked on one application written without any OO patterns, completely procedural, and it was a complete mess.

Adding or extending features caused cascading bugs. Eventually we had to rewrite the backend (PHP) into MVC to be productive. I quit because I dreaded the work. I didn't want to write another line.

I have been pretty interested in functional programming lately, or the functional paradigm everyone in JS is following nowadays, but haven't gotten the chance to take a deep dive yet.


I have reasons to believe that using "traditional OOP principles" is a bad way to program in JS. If you look at TypeScript development history, they started from adding classes to JS, but now work on advanced stuff that has nothing to do with OOP.


Do not do this. Do not listen to anyone calling JavaScript a "chaotic disaster zone", or engaging in any such 1990s style language-wars childishness.

I spent years programming C# before switching to JS, and it took me a long time to stop thinking in a object-oriented way and embrace the functional style that works best with JavaScript.

If you want to learn JS and be a good JS programmer, you are best learning JS, not some other language. That's basic common sense. A great place to start is with the "You Don't Know JS" book series, which are all available online, for free: https://github.com/getify/You-Dont-Know-JS.


If you want to be a good programmer, pick another language, and start there. you will learn more good practices from starting with a modern, full-featured language like python, than trying to work in the the chaotic disaster zone that is javascript.

This is just wrong on so many levels. I wish "we" would get over this meme that by picking up language X that is a paradigm shift from language Y, that you're going to be a better programmer in language Y. That's just nonsense.

The comment about "chaotic disaster zone" is just bitterness for the typical reasons of hatred for JavaScript.


I still consider myself to be a noob but I use a lot of these everyday. For example currying instead of binding. Am I just being humble?


You're obviously humble. If you know good functional programming practices from elsewhere, you might still be a noob when it comes to the JS ecosystem though.


No you're a noob


Where's the "Advanced" part? This is just JavaScript fundamentals.


You underestimate the JS populace


Do you mean overestimate?


Or underestimate the spread of Javascript over the population, which necessarily includes more and more people less familiar with programming :-)


I was about to post the same. This wasn't very advanced even in 2008. Also the word "ninja" is overused.


It's amazing to see what was "advanced" JavaScript in 2008 haha.


> #2: Goal: To be able to understand this function: > > // The .bind method from Prototype.js > Function.prototype.bind = function(){ > var fn = this, args = > Array.prototype.slice.call(arguments), object = args.shift(); > return function(){ > return fn.apply(object, > args.concat(Array.prototype.slice.call(arguments))); > }; > }

I do not want to understand this absurdness. I want a comment that tells me what it does so I can rewrite it in a sane way.


Do you really think it's good practice to use comments to describe how the language itself works? It's not like you can't find comprehensive, quality documentation for every single thing that happens in that function on MDN or equivalent.

Is that function that insane? I can understand it at a glance, and performance is high (it matters here). AS a JS programmer, just knowing that it is an implementation of bind() should be enough of a hint that it uses its first argument as context, the rest as a curry and the arguments of the newly generated function as additional arguments.


To be fair, the syntax of the variable initialization could be separated into 3 lines for better clarity.


This is a perfectly straightforward implementation of a somewhat sophisticated concept.

But one wouldn't encounter this code in an application; this is library code. It's so useful and common that it's part of the ES5 standard (yes, ES5, not ES6). It might be the bind method from Prototype.js, but it's also the .bind method that all ES5 JS functions have.

Anyway, the question is not whether or not you should/coudl write this. The question is whether or not it's a good goal to be able to read this.

IMO, anyone who cannot read this function, albeit slowly and carefully, isn't an intermediate JS programmer, much less an advanced one.

Which of the concepts in this chunk of code is foreign to you? Prototypes? this? Using Array.p.s.c() to turn array-like args into an actual Array? shift? returning a function that, when called, invokes another function on the concatenation of one set of args with another set of args? This is all easy-mode stuff, unless you're brand new to JS, or you haven't bothered learning it.


I learned about all of this stuff by doing challenges on codewars.com


The post is about learning advanced JavaScript, if you don't want to understand that then the post isn't for you, (not that there's anything wrong with that). There may not be a more sane way to write this, (in es5).


I would make 3 changes.

Separate the declaration to 3 lines because the 2nd and the 3rd declarations depend on each other. Keeping them on the same line makes the reader think this is a simply declarative code but in fact it's the opposite.

Avoid using the slice shift combo but use slice with a start of 1 plus direct index access to the arguments[0] to get the first argument. this way i'm not mutating the args array and that makes the code easier to follow.

Rename the object variable to '_this'. Giving the reader a hint as to what it is used for.

And of course if this is actually to be used in production I assume it will be wrapped inside a check to see if the bind function is already defined.


Hint: to write code in HN comments indent anything you write by at least two spaces. Result (long lines split - people on mobile may still have to scroll horizontally):

  // The .bind method from Prototype.js
  Function.prototype.bind = function(){
    var fn = this,
        args = Array.prototype.slice.call(arguments),
        object = args.shift();

    return function(){
      return fn.apply(
        object,
        args.concat(Array.prototype.slice.call(arguments))
      );
    };
  }


What's a more sane way to write that? It seems fairly straightforward to me, although ES6 might clean up the syntax a bit.


> I do not want to understand this absurdness.

Then don't read the ejohn? =)

(Me neither, fwiw. Would rather write a combined com/transpiler from sane to asm.js/wasm bypassing JS entirely. Not that I do, just that I'd rather.)


When you start using ES6, a lot of this garbage tends to disappear, if that helps. It infuriates me to see this kind of code, also. ES6 is a real godsend, dude! Fuck, I basically just said what everyone else here said. Well, whatever!


VM213:1 Uncaught ReferenceError: assert is not defined(…)





Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: