Revisions
-
creationix revised this gist
Aug 11, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -21,7 +21,7 @@ function createPerson(name) { Brian = createPerson('brian'); Tim = createPerson('tim'); // 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) -
creationix renamed this gist
Aug 11, 2010 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
creationix revised this gist
Aug 11, 2010 . 1 changed file with 49 additions and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -4,4 +4,52 @@ Person.create = function(name) { return new Person(name) }; Brian = new Person('brianleroux'); Tim = Person.create('tim'); // 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"; -
creationix revised this gist
Aug 11, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -4,4 +4,4 @@ Person.create = function(name) { return new Person(name) }; Brian = new Person('brianleroux'); Tim = Person.create('tim'); // Lame -
brianleroux created this gist
Aug 11, 2010 .There are no files selected for viewing
This file contains 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,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!