This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addClass = function(className, element) { | |
element.className += ' ' + className; | |
return element; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var map = function(callback, array) { | |
var newArray = []; | |
for (var i = 0; i < array.length; i = i + 1) { | |
newArray[i] = callback(array[i], i); | |
} | |
return newArray; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ids = ['DEE', 'DUM']; | |
// (chưa cần hiểu bind vội) Mục đích là để thu được các thẻ DOM có id là “DEE”, “DUM” | |
var elements = map(document.getElementById.bind(document), ids); | |
// thêm tên class cho các thẻ DOM thu được | |
map(addClass, elements); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addTweedleClass = function(el) { | |
return addClass('tweedle', el); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ids = ['DEE', 'DUM']; | |
var elements = map(document.getElementById.bind(document), ids); | |
elements = map(addTweedleClass, elements); // 🌟 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addBoyClass = function(el) { | |
return addClass('boy', el); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var partialFirstOfTwo = function(fn, param1) { | |
return function(param2) { | |
return fn(param1, param2); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addTweedleClass = partialFirstOfTwo(addClass, 'tweedle'); | |
var addBoyClass = partialFirstOfTwo(addClass, 'boy'); | |
var ids = ['DEE', 'DUM']; | |
var elements = map(document.getElementById.bind(document), ids); | |
elements = map(addTweedleClass, elements); | |
elements = map(addBoyClass, elements); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var argsToArray = function(args) { | |
return Array.prototype.slice.call(args, 0); | |
} | |
var partial = function() { | |
// chuyển đổi đối-tượng-giống-mảng sang một mảng mới | |
var args = argsToArray(arguments); | |
// shift() cho phép lấy ra phần tử đầu tiên của mảng. Trong trường hợp này phần tử đầu tiên là một hàm. args sẽ chứa các các đối số còn lại. | |
var fn = args.shift(); // 🐘 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var twinkle = function(noun, wonderAbout) { | |
return 'Twinkle, twinkle, little ' + | |
noun + '\nHow I wonder where you ' + | |
wonderAbout; | |
} | |
// fix-cứng noun | |
var twinkleBat = partial(twinkle, 'bat'); | |
twinkleBat('are at'); // Twinkle, twinkle, little bat\nHow I wonder where you are at |