-
-
Save chip/8cda61d5b4dc06a1df15 to your computer and use it in GitHub Desktop.
Revisions
-
benpriebe revised this gist
Oct 1, 2014 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,8 @@ Douglas Crockford showed a slide showing how he creates JavaScript objects in 2014. He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes. https://www.youtube.com/watch?v=bo36MrBfTk4 (skip ahead to 35 mins for relevant section) Here is the pattern described on the slide: -
benpriebe revised this gist
Oct 1, 2014 . 1 changed file with 9 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ https://www.youtube.com/watch?v=bo36MrBfTk4 (35 mins in) Here is the pattern described on the slide: ``` function constructor(spec) { var that = otherConstructor(spec), member, @@ -15,10 +15,11 @@ function constructor(spec) { that.method = method; return that; } ``` Here is my interpretation of this pattern: ``` function Person(spec) { var person = spec; @@ -31,7 +32,9 @@ function Person(spec) { return person.firstName + " " + person.lastName; } } ``` ``` function Employee(spec) { var employee = Person(spec); @@ -50,8 +53,9 @@ function Employee(spec) { return employee.hourlyRate * hoursWorked; } } ``` ``` var ben = Employee({ firstName: 'Ben', lastName: 'Priebe', @@ -60,4 +64,5 @@ var ben = Employee({ }); console.log(ben.getDisplayName()); console.log(ben.calculatePay(38)); ``` -
benpriebe revised this gist
Oct 1, 2014 . 2 changed files with 0 additions and 63 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,63 +0,0 @@ File renamed without changes. -
benpriebe revised this gist
Oct 1, 2014 . 1 changed file with 63 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ Douglas showed a slide showing how he creates JavaScript objects in 2014. He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes. https://www.youtube.com/watch?v=bo36MrBfTk4 (35 mins in) Here is the pattern described on the slide: ''' function constructor(spec) { var that = otherConstructor(spec), member, method = function () { // spec, member, method }; that.method = method; return that; } ''' Here is my interpretation of this pattern: function Person(spec) { var person = spec; // methods person.getDisplayName = getDisplayName; return person; function getDisplayName() { return person.firstName + " " + person.lastName; } } function Employee(spec) { var employee = Person(spec); // members employee.employeeId = spec.employeeId; employee.hourlyRate = spec.hourlyRate; // methods employee.calculatePay = calculatePay; return employee; // implementations function calculatePay(hoursWorked) { return employee.hourlyRate * hoursWorked; } } var ben = Employee({ firstName: 'Ben', lastName: 'Priebe', hourlyRate: 120, employeeId: 1 }); console.log(ben.getDisplayName()); console.log(ben.calculatePay(38)); -
benpriebe revised this gist
Oct 1, 2014 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,11 @@ Douglas showed a slide showing how he creates JavaScript objects in 2014. He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes. https://www.youtube.com/watch?v=bo36MrBfTk4 (35 mins in) Here is the pattern described on the slide: ''' function constructor(spec) { var that = otherConstructor(spec), member, @@ -14,6 +15,7 @@ function constructor(spec) { that.method = method; return that; } ''' Here is my interpretation of this pattern: -
benpriebe revised this gist
Oct 1, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ //Douglas showed a slide showing how he creates JavaScript objects in 2014. //He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes. https://www.youtube.com/watch?v=bo36MrBfTk4 (35 mins in) //Here is the pattern described on the slide: function constructor(spec) { var that = otherConstructor(spec), -
benpriebe revised this gist
Oct 1, 2014 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,10 @@ Douglas showed a slide showing how he creates JavaScript objects in 2014. He no longer uses Object.create(), avoids 'this' and doesn't even care about memory reduction by using prototypes. https://www.youtube.com/watch?v=bo36MrBfTk4 (35 mins in) Here is the pattern described on the slide: function constructor(spec) { var that = otherConstructor(spec), member, @@ -8,6 +15,8 @@ function constructor(spec) { return that; } Here is my interpretation of this pattern: function Person(spec) { var person = spec; -
benpriebe renamed this gist
Oct 1, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
benpriebe created this gist
Oct 1, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ function constructor(spec) { var that = otherConstructor(spec), member, method = function () { // spec, member, method }; that.method = method; return that; } function Person(spec) { var person = spec; // methods person.getDisplayName = getDisplayName; return person; function getDisplayName() { return person.firstName + " " + person.lastName; } } function Employee(spec) { var employee = Person(spec); // members employee.employeeId = spec.employeeId; employee.hourlyRate = spec.hourlyRate; // methods employee.calculatePay = calculatePay; return employee; // implementations function calculatePay(hoursWorked) { return employee.hourlyRate * hoursWorked; } } var ben = Employee({ firstName: 'Ben', lastName: 'Priebe', hourlyRate: 120, employeeId: 1 }); console.log(ben.getDisplayName()); console.log(ben.calculatePay(38));