Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="CH 4 functions" content="Javascript Patterns">
  5. <meta charset="utf-8">
  6. <title>JS Bin</title>
  7. </headJavascript patterns
  8. <body>
  9. Javascript Patterns, by Stoyan Stefanov (O'Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750.
  10. <script id="jsbin-javascript">
  11. //My crib notes from "JavaScript Patterns" by Stoyan Stefanov
  12. //Chapter 4 functions
  13. //includes some examples too
  14. //you will need to buy the book - these notes are sketchy and for review & reference only
  15.  
  16.  
  17. "use strict";
  18.  
  19. //functions are first class objects
  20. //you can delete them, augment them, assign them
  21. //references copied to other objects
  22. //can have their own methods and properties, pass as arguments,
  23. //return them from other functions
  24. //can be created dynamically at runtime
  25. //provide scope - there's only function scope
  26.  
  27.  
  28. //Named function expressions
  29. var add = function add(a,b){
  30. return a + b;
  31. };
  32.  
  33. //this produces an unnamed function expression
  34. var add = function(a,b){
  35. return a + b;
  36. };
  37. //also function declarations
  38. function zebra(){
  39. //
  40. }
  41.  
  42. //passing functions, using functions in object literals - these both
  43. //use function expressions
  44. //examples
  45. // callMe(function me(){
  46. //named function expression
  47. //});
  48.  
  49. var x = {
  50. yahoo: function(){
  51. //function expression
  52. }
  53. };
  54.  
  55. //function declarations can only be defined in global scope or function scope
  56. // they can't be assigned to other variables or used as parameters when
  57. // calling functions
  58. //example
  59. function fooBar(){
  60. return 1;
  61. }
  62.  
  63. var addEm = function(a,b){
  64. return a() + b;
  65. };
  66. console.log(addEm(fooBar, 2));
  67.  
  68. //but this won't work as expected
  69. console.log(add(fooBar,2));
  70.  
  71.  
  72. //function name property - useful for debugging, recursive calls
  73. // if this not needed then anonymous function expressions less verbose
  74.  
  75. //Function Hoisting
  76. //here lies a difference between function declarations and named function
  77. //expressions
  78.  
  79. //var z hoisted to top of scope during 'parsing' or
  80. //'compiling' - so be careful of calling it before the function expression
  81. //is defined
  82.  
  83. function z(){
  84. return "global z";
  85. }
  86.  
  87. function y(){
  88. return "global y";
  89. }
  90.  
  91. console.log(z());
  92.  
  93. function holyHoisting(){
  94.  
  95. console.log(y());
  96. //connsole.log(z()); typeerror: z is not a function
  97.  
  98. var z = function z(){
  99. return "local z";
  100. };
  101. function y(){
  102. return "holy Hoisting Batman!";
  103. }
  104.  
  105. }
  106.  
  107. holyHoisting();
  108.  
  109.  
  110. //Callback Pattern
  111. //callbacks and scope
  112.  
  113. //if callback uses 'this' - what 'this' refers to could be the global
  114. //object or the DOM object - incorrectly.
  115. //remedy - use call with parameters = callback name and the object you
  116. //want 'this' to refer to
  117.  
  118. //examples
  119. var myOjb = {
  120. paint: function(){
  121. return this.color;
  122. },
  123. color: "green"
  124. };
  125.  
  126. //to use this instead of function(callback)
  127. //use function(callback, callback_ojb)
  128.  
  129. var findNodes = function(callback, callback_obj){
  130. //...
  131. if(typeof callback === 'function'){
  132. callback.call(callback_obj);
  133. }
  134. //...
  135. };
  136.  
  137. //Or
  138. var findNodes = function(callback, callback_obj){
  139. //...
  140. if(typeof callback === 'string'){
  141. callback = callback_obj[callback];
  142. }
  143.  
  144. if(typeof callback === 'function'){
  145. callback.call(callback_obj);
  146. }
  147. //...
  148. };
  149.  
  150. //returning functions
  151. // example of a function that counts everytime you call it
  152. var counter = function(){
  153. var count = 0;
  154. return function(){
  155. console.log(count);
  156. count++;
  157. };
  158. };
  159.  
  160. var next = counter();
  161. for(var i = 0; i < 3; i++){
  162. next();
  163. }
  164.  
  165. //self-defining functions
  166. //example
  167.  
  168. var myOwnDef = function(){
  169. console.log("I'm doing some initial startup work");
  170. myOwnDef = function(){
  171. console.log("Now I always do this thing");
  172. };
  173. };
  174.  
  175. //myOwnDef();
  176. //myOwnDef();
  177.  
  178.  
  179.  
  180. //assign properties to the function?
  181. myOwnDef.readStr = "new property";
  182. //what if someone does this?
  183. var useMyOwnDef = myOwnDef;
  184.  
  185. //never redefines the myOwnDef function
  186. useMyOwnDef();
  187. useMyOwnDef();
  188.  
  189. console.log(useMyOwnDef.readStr);
  190. console.log(myOwnDef.readStr);
  191.  
  192. var myOwnSetUp = function(){
  193. console.log('set UP a read string property');
  194. myOwnSetUp = function(string){
  195. console.log(string);
  196. };
  197. };
  198.  
  199. myOwnSetUp.readStr = "new input string";
  200.  
  201. myOwnSetUp();
  202. myOwnSetUp(myOwnSetUp.readStr);
  203.  
  204.  
  205. //Immediate functions
  206. //execute function immediately after it's defined
  207. //parameters of an immediate function
  208. //example: global passed in
  209.  
  210. //var o = function(global){
  211. //
  212. //}(this);
  213.  
  214. var global = this;
  215. //returned values from an immediate function
  216. var o = {
  217. name: (function(g){
  218. return g.name;
  219. }(global)),
  220. message : function(){
  221. return "global name: " + this.name;
  222. }
  223.  
  224. };
  225.  
  226. console.log(o.message());
  227.  
  228. //immediate object initialization
  229. //example
  230.  
  231. ({
  232. init: function(){
  233. console.log(this.message);
  234. },
  235. message: "i'm ok and you're ok!"
  236.  
  237. }).init();
  238.  
  239. //() serves as a grouping operator above - saying it's an object literal
  240. //not a block of code
  241. //not polluting global namespace
  242. //suitable to one - off tasks
  243. //local helper functions
  244. //may not minify well
  245. //useful pattern to use when doing tasks like browser sniffing -
  246.  
  247. // memoization pattern
  248. // use function properties to cache function results
  249. //example
  250.  
  251.  
  252. var memoized = function(){
  253. //create a cache key - jason stringify if it's not primitive type
  254. var paramArray = Array.prototype.slice.call(arguments),
  255. cacheKey = JSON.stringify(paramArray),
  256. length,
  257. result;
  258.  
  259. //if result is cached return it
  260. if(!memoized.cached[cacheKey]){
  261.  
  262. //.... expensive steps here
  263. length = paramArray.length;
  264. result = 0;
  265. console.log(length);
  266. for(var j = 0; j < length; j += 1){
  267. result += paramArray[j]* 3.141592654;
  268.  
  269. }
  270. console.log('finished calc loop');
  271. memoized.cached[cacheKey] = result;
  272. }
  273. return memoized.cached[cacheKey];
  274.  
  275. };
  276. memoized.cached = {};
  277.  
  278.  
  279. console.log(memoized(1.0,2.0,3.0,4.0,5,6,7,8,9,10));
  280. console.log(memoized(1.0,2.0,3.0,4.0,5,6,7,8,9,10));
  281.  
  282. //Configuration Objects
  283. //use a configuration object instead of long parameter lists - maybe with
  284. //some optional and non optional parameters
  285.  
  286. //example
  287.  
  288. var conf = {
  289. userName : "ddnoname",
  290. firstName: "dee",
  291. lastName: "for"
  292. };
  293.  
  294. //call function
  295. // f(conf);
  296. //rather than for example:
  297. // f(firstName,lastName, null, null, userName);
  298.  
  299. //but parameters cannot be minified
  300.  
  301. //Curry
  302. // partial function application
  303. // function application
  304. // Function.prototype.apply()
  305. // Function.prototype.call()
  306.  
  307. //partial application
  308. // returns a function
  309.  
  310. //Currying
  311. // like turning add(5,4) into add(5)(4)
  312.  
  313. //example
  314. //accepts a partial list of args
  315.  
  316. function add(x1,y1) {
  317. var oldx = x1,
  318. oldy = y1;
  319. if(typeof oldy === "undefined"){
  320. return function(newy){
  321. return oldx + newy;
  322. };
  323. }
  324. return x1 + y1;
  325. }
  326.  
  327. console.log(add(4)(5));
  328.  
  329. var addPI = add(3.141592654);
  330. console.log(addPI(1000));
  331. </script>
  332.  
  333.  
  334.  
  335. <script id="jsbin-source-javascript" type="text/javascript">//My crib notes from "JavaScript Patterns" by Stoyan Stefanov
  336. //Chapter 4 functions
  337. //includes some examples too
  338. //you will need to buy the book - these notes are sketchy and for review & reference only
  339.  
  340.  
  341. "use strict";
  342.  
  343. //functions are first class objects
  344. //you can delete them, augment them, assign them
  345. //references copied to other objects
  346. //can have their own methods and properties, pass as arguments,
  347. //return them from other functions
  348. //can be created dynamically at runtime
  349. //provide scope - there's only function scope
  350.  
  351.  
  352. //Named function expressions
  353. var add = function add(a,b){
  354. return a + b;
  355. };
  356.  
  357. //this produces an unnamed function expression
  358. var add = function(a,b){
  359. return a + b;
  360. };
  361. //also function declarations
  362. function zebra(){
  363. //
  364. }
  365.  
  366. //passing functions, using functions in object literals - these both
  367. //use function expressions
  368. //examples
  369. // callMe(function me(){
  370. //named function expression
  371. //});
  372.  
  373. var x = {
  374. yahoo: function(){
  375. //function expression
  376. }
  377. };
  378.  
  379. //function declarations can only be defined in global scope or function scope
  380. // they can't be assigned to other variables or used as parameters when
  381. // calling functions
  382. //example
  383. function fooBar(){
  384. return 1;
  385. }
  386.  
  387. var addEm = function(a,b){
  388. return a() + b;
  389. };
  390. console.log(addEm(fooBar, 2));
  391.  
  392. //but this won't work as expected
  393. console.log(add(fooBar,2));
  394.  
  395.  
  396. //function name property - useful for debugging, recursive calls
  397. // if this not needed then anonymous function expressions less verbose
  398.  
  399. //Function Hoisting
  400. //here lies a difference between function declarations and named function
  401. //expressions
  402.  
  403. //var z hoisted to top of scope during 'parsing' or
  404. //'compiling' - so be careful of calling it before the function expression
  405. //is defined
  406.  
  407. function z(){
  408. return "global z";
  409. }
  410.  
  411. function y(){
  412. return "global y";
  413. }
  414.  
  415. console.log(z());
  416.  
  417. function holyHoisting(){
  418.  
  419. console.log(y());
  420. //connsole.log(z()); typeerror: z is not a function
  421.  
  422. var z = function z(){
  423. return "local z";
  424. };
  425. function y(){
  426. return "holy Hoisting Batman!";
  427. }
  428.  
  429. }
  430.  
  431. holyHoisting();
  432.  
  433.  
  434. //Callback Pattern
  435. //callbacks and scope
  436.  
  437. //if callback uses 'this' - what 'this' refers to could be the global
  438. //object or the DOM object - incorrectly.
  439. //remedy - use call with parameters = callback name and the object you
  440. //want 'this' to refer to
  441.  
  442. //examples
  443. var myOjb = {
  444. paint: function(){
  445. return this.color;
  446. },
  447. color: "green"
  448. };
  449.  
  450. //to use this instead of function(callback)
  451. //use function(callback, callback_ojb)
  452.  
  453. var findNodes = function(callback, callback_obj){
  454. //...
  455. if(typeof callback === 'function'){
  456. callback.call(callback_obj);
  457. }
  458. //...
  459. };
  460.  
  461. //Or
  462. var findNodes = function(callback, callback_obj){
  463. //...
  464. if(typeof callback === 'string'){
  465. callback = callback_obj[callback];
  466. }
  467.  
  468. if(typeof callback === 'function'){
  469. callback.call(callback_obj);
  470. }
  471. //...
  472. };
  473.  
  474. //returning functions
  475. // example of a function that counts everytime you call it
  476. var counter = function(){
  477. var count = 0;
  478. return function(){
  479. console.log(count);
  480. count++;
  481. };
  482. };
  483.  
  484. var next = counter();
  485. for(var i = 0; i < 3; i++){
  486. next();
  487. }
  488.  
  489. //self-defining functions
  490. //example
  491.  
  492. var myOwnDef = function(){
  493. console.log("I'm doing some initial startup work");
  494. myOwnDef = function(){
  495. console.log("Now I always do this thing");
  496. };
  497. };
  498.  
  499. //myOwnDef();
  500. //myOwnDef();
  501.  
  502.  
  503.  
  504. //assign properties to the function?
  505. myOwnDef.readStr = "new property";
  506. //what if someone does this?
  507. var useMyOwnDef = myOwnDef;
  508.  
  509. //never redefines the myOwnDef function
  510. useMyOwnDef();
  511. useMyOwnDef();
  512.  
  513. console.log(useMyOwnDef.readStr);
  514. console.log(myOwnDef.readStr);
  515.  
  516. var myOwnSetUp = function(){
  517. console.log('set UP a read string property');
  518. myOwnSetUp = function(string){
  519. console.log(string);
  520. };
  521. };
  522.  
  523. myOwnSetUp.readStr = "new input string";
  524.  
  525. myOwnSetUp();
  526. myOwnSetUp(myOwnSetUp.readStr);
  527.  
  528.  
  529. //Immediate functions
  530. //execute function immediately after it's defined
  531. //parameters of an immediate function
  532. //example: global passed in
  533.  
  534. //var o = function(global){
  535. //
  536. //}(this);
  537.  
  538. var global = this;
  539. //returned values from an immediate function
  540. var o = {
  541. name: (function(g){
  542. return g.name;
  543. }(global)),
  544. message : function(){
  545. return "global name: " + this.name;
  546. }
  547.  
  548. };
  549.  
  550. console.log(o.message());
  551.  
  552. //immediate object initialization
  553. //example
  554.  
  555. ({
  556. init: function(){
  557. console.log(this.message);
  558. },
  559. message: "i'm ok and you're ok!"
  560.  
  561. }).init();
  562.  
  563. //() serves as a grouping operator above - saying it's an object literal
  564. //not a block of code
  565. //not polluting global namespace
  566. //suitable to one - off tasks
  567. //local helper functions
  568. //may not minify well
  569. //useful pattern to use when doing tasks like browser sniffing -
  570.  
  571. // memoization pattern
  572. // use function properties to cache function results
  573. //example
  574.  
  575.  
  576. var memoized = function(){
  577. //create a cache key - jason stringify if it's not primitive type
  578. var paramArray = Array.prototype.slice.call(arguments),
  579. cacheKey = JSON.stringify(paramArray),
  580. length,
  581. result;
  582.  
  583. //if result is cached return it
  584. if(!memoized.cached[cacheKey]){
  585.  
  586. //.... expensive steps here
  587. length = paramArray.length;
  588. result = 0;
  589. console.log(length);
  590. for(var j = 0; j < length; j += 1){
  591. result += paramArray[j]* 3.141592654;
  592.  
  593. }
  594. console.log('finished calc loop');
  595. memoized.cached[cacheKey] = result;
  596. }
  597. return memoized.cached[cacheKey];
  598.  
  599. };
  600. memoized.cached = {};
  601.  
  602.  
  603. console.log(memoized(1.0,2.0,3.0,4.0,5,6,7,8,9,10));
  604. console.log(memoized(1.0,2.0,3.0,4.0,5,6,7,8,9,10));
  605.  
  606. //Configuration Objects
  607. //use a configuration object instead of long parameter lists - maybe with
  608. //some optional and non optional parameters
  609.  
  610. //example
  611.  
  612. var conf = {
  613. userName : "ddnoname",
  614. firstName: "dee",
  615. lastName: "for"
  616. };
  617.  
  618. //call function
  619. // f(conf);
  620. //rather than for example:
  621. // f(firstName,lastName, null, null, userName);
  622.  
  623. //but parameters cannot be minified
  624.  
  625. //Curry
  626. // partial function application
  627. // function application
  628. // Function.prototype.apply()
  629. // Function.prototype.call()
  630.  
  631. //partial application
  632. // returns a function
  633.  
  634. //Currying
  635. // like turning add(5,4) into add(5)(4)
  636.  
  637. //example
  638. //accepts a partial list of args
  639.  
  640. function add(x1,y1) {
  641. var oldx = x1,
  642. oldy = y1;
  643. if(typeof oldy === "undefined"){
  644. return function(newy){
  645. return oldx + newy;
  646. };
  647. }
  648. return x1 + y1;
  649. }
  650.  
  651. console.log(add(4)(5));
  652.  
  653. var addPI = add(3.141592654);
  654. console.log(addPI(1000));
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684. </script></body>
  685. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement