Master the JavaScript Interview: What is Functional Programming?
- Pure functions
- Given the same inputs, always returns the same output, and
- Has no side-effects
- Function composition
- composition over imperative flow control
- Avoid shared state
| const body = document.body; | |
| const draggableElement = document.getElementById('drag'); | |
| const mouseDown$ = Rx.Observable.fromEvent(draggableElement, 'mousedown'); | |
| const mouseUp$ = Rx.Observable.fromEvent(body, 'mouseup'); | |
| const mouseMove$ = Rx.Observable.fromEvent(body, 'mousemove'); | |
| mouseDown$ | |
| .map(event => mouseMove$.takeUntil(mouseUp$)) | |
| .concatAll() |
| @mixin for-size($range) { | |
| $phone-upper-boundary: 600px; | |
| $tablet-portrait-upper-boundary: 900px; | |
| $tablet-landscape-upper-boundary: 1200px; | |
| $desktop-upper-boundary: 1800px; | |
| @if $range == phone-only { | |
| @media (max-width: #{$phone-upper-boundary - 1}) { @content; } | |
| } @else if $range == tablet-portrait-up { | |
| @media (min-width: $phone-upper-boundary) { @content; } |
Master the JavaScript Interview: What is Functional Programming?
| const a = Object.freeze({ | |
| foo: 'Hello', | |
| bar: 'world', | |
| baz: '!' | |
| }); | |
| a.foo = 'Goodbye'; | |
| // Error: Cannot assign to read only property 'foo' of object Object |
| const getTextFeature = (text, color) => { | |
| try { | |
| const canvas = document.createElement('canvas'); | |
| /* | |
| 因为进行scale以后的图案区域实际上不能确定, | |
| 理论上应该只在(0,0,1,1),但有的也会在它周围的像素里, | |
| 综合效率的考虑,给一个2*2的范围是比较合适的; | |
| */ | |
| canvas.width = 2; | |
| canvas.height = 2; |
| var event = document.createEvent('MouseEvent'); | |
| var args = ['click', true, true]; | |
| event.data = { 'foo': 'bar' }; | |
| event.initEvent.apply(event, args); | |
| element.dispatchEvent(event); |
| var Container = function (x) { | |
| this.__value = x; | |
| }; | |
| Container.of = function (x) { | |
| return new Container(x); | |
| }; | |
| Container.of(3); |
| var compose = function (f, g) { | |
| return function (x) { | |
| return f(g(x)); | |
| }; | |
| }; | |
| var toUpperCase = x => x.toUpperCase(); | |
| var exclaim = x => x + '!'; | |
| var shout = compose(exclaim, toUpperCase); |
| var curry = _.curry; | |
| var match = curry((what, str) => str.match(what)); | |
| var replace = curry((what, replacement, str) => str.replace(what, replacement)); | |
| var filter = curry((f, ary) => ary.filter(f)); | |
| var map = curry((f, ary) => ary.map(f)); | |
| match(/\s+/g, 'hello world'); | |
| // [" "] | |
| match(/\s+/g)('hello world'); |
| // | ------------- | ------------ | ------------------------------------------ | | |
| // | z-index range | content type | details | | |
| // | ------------- | ------------ | ------------------------------------------ | | |
| // | < 0 | Background | | | |
| // | | Elements | | | |
| // | ------------- | ------------ | ------------------------------------------ | | |
| // | 0 - 4,999 | Main Content | Standard ad tags in place with regular | | |
| // | | Standard Ads | content. Includes OBA Self Regulation | | |
| // | | | Message (CLEAR Ad Notice) | | |
| // | ------------- | ------------ | ------------------------------------------ | |