Javascript is not going away, it had been here since 1994, and after 18 years, it is still here, all the popular frameworks and libraries such as JQuery, AngularJS are all built on top of Javascript. I strongly encourage anyone to learn JavaScript properly instead of scrapping and learning bit and pieces from little scripts on the internet. A good start is to read Douglas Crockford’s JavaScript: The Good Parts Unearthing the Excellence in JavaScript. Douglas as you may aware of, is known for JSON.
I am half way through the book. To be frank, I absolutely loved the book and I hope to finish it soon as it contains 170-ish page only. I was always trying to find a book that is thin but still manages to cover the crucial fundamentals that I need to remember, and this book does it perfectly by not including the bad parts of Javascript that we should not learn anyway (quite difficult to unlearn stuff).
Some comments on the examples in the book:
Page 84 | Chapter 8: Methods
Function
function.apply(thisArg, argArray)
The apply method invokes a function, passing in the object that will be bound to this and an optional array of arguments. The apply method is used in the apply invocation pattern (Chapter 4):
Function.method('bind', function (that) {
// Return a function that will call this function as
// though it is a method of that object.
var method = this,
slice = Array.prototype.slice,
args = slice.apply(arguments, [1]);
return function () {
return method.apply(that,
args.concat(slice.apply(arguments, [0]))
);
};
});
var x = function () {
return this.value;
}.bind(
{value: 666}
);
alert(x()); // 666
I was confused with the example as first, because of the unnecessary code, which could be simplify to:
Function.method('bind1', function (that) {
// Return a function that will call this function as
// though it is a method of that object.
var method = this;
return function () {
return method.apply(that, arguments);
};}
);
Second thought, it might be because the author wanted it to be generic enough for code reuse, but it does not seems so, as you may notice, slice.apply(arguments, ...);
is executed twice and then concatenated together, with hard-coded argArrays [0] and [1].
Let me know if you know what did he intended to do.