Skip to content

Instantly share code, notes, and snippets.

@creationix
Forked from brianleroux/wtftim.js
Created August 11, 2010 20:38

Revisions

  1. creationix revised this gist Aug 11, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion newisevil.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ function createPerson(name) {
    Brian = createPerson('brian');
    Tim = createPerson('tim');

    // Prototypal. However, sometimes you need raw speed, or want to inherit from another object.
    // However, sometimes you need raw speed, or want to inherit from another object.
    // In that case prototypal objects are simple and perfect.

    // Make a prototype of your objects (aka class)
  2. creationix renamed this gist Aug 11, 2010. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. creationix revised this gist Aug 11, 2010. 1 changed file with 49 additions and 1 deletion.
    50 changes: 49 additions & 1 deletion wtftim.js
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,52 @@ Person.create = function(name) { return new Person(name) };
    Brian = new Person('brianleroux');
    Tim = Person.create('tim');

    // Lame
    // Lame, I would do this one of three ways depending on how it will be used

    // If the example really was this simple and there was nothing else involved,
    // I would use object literals. No need to overcomplicate things
    Brian = {name: "brian"};
    Tim = {name: "tim"};

    // Assuming there was a reason for more complicated encapsulation, the "best" in some ways
    // is a closure that returns objects. Then you can have true private functions and variables
    // and there is no need for "this" and "bind" all over the place.
    function createPerson(name) {
    return {name: name};
    }

    Brian = createPerson('brian');
    Tim = createPerson('tim');

    // Prototypal. However, sometimes you need raw speed, or want to inherit from another object.
    // In that case prototypal objects are simple and perfect.

    // Make a prototype of your objects (aka class)
    var Person = {};

    // And make a constructor factory that creates these things for you
    function createPerson(name) {
    var person = Object.create(Person);
    person.name = name;
    return person;
    }

    Brian = createPerson('brian');
    Tim = createPerson('tim');

    // Also you can put the initialize code in the prototype itself so that it can be inherited later on.
    var Person = {
    initialize: function (name) {
    this.name = name;
    }
    };

    Brian = Object.create(Person);
    Brian.initialize('brian');
    Tim = Object.create(Person);
    Tim.initialize('tim');

    // And a "subclass" of Person is the exact same thing as an "instance" of Person.
    // You simply inherit and specialize
    Programmer = Object.create(Person);
    Programmer.hobby = "hack";
  4. creationix revised this gist Aug 11, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wtftim.js
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@ Person.create = function(name) { return new Person(name) };
    Brian = new Person('brianleroux');
    Tim = Person.create('tim');

    // same fukin difference!
    // Lame
  5. @brianleroux brianleroux created this gist Aug 11, 2010.
    7 changes: 7 additions & 0 deletions wtftim.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    var Person = function( name ){ this.name = name };
    Person.create = function(name) { return new Person(name) };

    Brian = new Person('brianleroux');
    Tim = Person.create('tim');

    // same fukin difference!