File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed
Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 3838
3939// find the nodes and hide them as you go
4040findNodes ( hide ) ;
41+
42+
4143</ script >
4244</ body >
4345</ html >
Original file line number Diff line number Diff line change 1+ <!doctype html>
2+ < html lang ="en ">
3+ < head >
4+ < title > JavaScript Patterns</ title >
5+ < meta charset ="utf-8 ">
6+ </ head >
7+ < body >
8+ < script >
9+ var conf = {
10+ username : "shichuan" ,
11+ first : "Chuan" ,
12+ last : "Shi"
13+ } ;
14+ addPerson ( conf ) ;
15+ </ script >
16+ </ body >
17+ </ html >
Original file line number Diff line number Diff line change 66</ head >
77< body >
88< script >
9+ /***
10+ function application
11+ ***/
12+ // define a function
13+ var sayHi = function ( who ) {
14+ return "Hello" + ( who ? ", " + who : "" ) + "!" ;
15+ } ;
16+
17+ // invoke a function
18+ sayHi ( ) ; // "Hello"
19+ sayHi ( 'world' ) ; // "Hello, world!"
20+
21+ // apply a function
22+ sayHi . apply ( null , [ "hello" ] ) ; // "Hello, hello!"
23+
24+ var alien = {
25+ sayHi : function ( who ) {
26+ return "Hello" + ( who ? ", " + who : "" ) + "!" ;
27+ }
28+ } ;
29+
30+ alien . sayHi ( 'world' ) ; // "Hello, world!"
31+ sayHi . apply ( alien , [ "humans" ] ) ; // "Hello, humans!"
32+
33+ // the second is more efficient, saves an array
34+ sayHi . apply ( alien , [ "humans" ] ) ; // "Hello, humans!"
35+ sayHi . call ( alien , "humans" ) ; // "Hello, humans!"
36+
37+ /***
38+ partial application
39+ ***/
40+
41+ // for illustration purposes
42+ // not valid JavaScript
43+
44+ // we have this function
45+ function add ( x , y ) {
46+ return x + y ;
47+ }
48+
49+ // and we know the arguments
50+ add ( 5 , 4 ) ;
51+
52+ // step 1 -- substitute one argument
53+ function add ( 5 , y ) {
54+ return 5 + y ;
55+ }
56+
57+ // step 2 -- substitute the other argument
58+ function add ( 5 , 4 ) {
59+ return 5 + 4 ;
60+ }
61+
62+ /***
63+ currying
64+ ***/
65+
966// a curried add()
1067// accepts partial list of arguments
1168function add ( x , y ) {
You can’t perform that action at this time.
0 commit comments