Skip to content

Instantly share code, notes, and snippets.

@chip
Forked from benpriebe/object-creation.md
Created February 23, 2016 01:56
Show Gist options
  • Save chip/8cda61d5b4dc06a1df15 to your computer and use it in GitHub Desktop.
Save chip/8cda61d5b4dc06a1df15 to your computer and use it in GitHub Desktop.

Revisions

  1. @benpriebe benpriebe revised this gist Oct 1, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions object-creation.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    Douglas showed a slide showing how he creates JavaScript objects in 2014.
    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 (35 mins in)
    https://www.youtube.com/watch?v=bo36MrBfTk4 (skip ahead to 35 mins for relevant section)

    Here is the pattern described on the slide:

  2. @benpriebe benpriebe revised this gist Oct 1, 2014. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions object-creation.md
    Original 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));
    console.log(ben.calculatePay(38));
    ```
  3. @benpriebe benpriebe revised this gist Oct 1, 2014. 2 changed files with 0 additions and 63 deletions.
    63 changes: 0 additions & 63 deletions object-creation
    Original file line number Diff line number Diff line change
    @@ -1,63 +0,0 @@
    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));
    File renamed without changes.
  4. @benpriebe benpriebe revised this gist Oct 1, 2014. 1 changed file with 63 additions and 0 deletions.
    63 changes: 63 additions & 0 deletions object-creation
    Original 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));
  5. @benpriebe benpriebe revised this gist Oct 1, 2014. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions create-objects
    Original 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.
    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:
    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:

  6. @benpriebe benpriebe revised this gist Oct 1, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions create-objects
    Original 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.
    //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:
    //Here is the pattern described on the slide:

    function constructor(spec) {
    var that = otherConstructor(spec),
  7. @benpriebe benpriebe revised this gist Oct 1, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions create-objects
    Original 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;

  8. @benpriebe benpriebe renamed this gist Oct 1, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @benpriebe benpriebe created this gist Oct 1, 2014.
    52 changes: 52 additions & 0 deletions gistfile1.js
    Original 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));