[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.
Alicea has React course called 'React and Flux for Angular Developers'in Pluralsight if anyone is looking more of his courses.
[0] http://dmitrysoshnikov.com/ecmascript/javascript-the-core/
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...
https://www.amazon.ca/Secrets-JavaScript-Ninja-John-Resig/dp...
"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... [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.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.
A good programmer can pick up javascript quickly. Other than syntactic and semantic warts, it is not a difficult language to learn.
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.
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.
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.
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.
// 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))
);
};
}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.)