Cedric, good patten, makes for a convenient factory method for making new instances - especially if you curry the prototype.
-- Anatoly Geyfman http://www.geyfman.net On Thursday, November 17, 2011 at 1:29 AM, Cédric Bertolini wrote: > Hi, > > Here is a pattern i'm using frequently: > > var MyClass = (function() { > var prototype = { /* shared attributes */ }; > var constructor = function () { /* instance properties and > bootstrapping */ }; > constructor.prototype = prototype; > return constructor; > })(); > > In your case: > > var Person =(function() { > var prototype = { describe: function(){alert('NAME IS '+this.name > (http://+this.name))} }; > var constructor = function Person(name) { this.name (http://this.name) = > name; }; > constructor.prototype = prototype; > return constructor; > })(); > var jane = new Person('JANE'); > var tarzan = new Person('TARZAN'); > jane.describe(); > tarzan.describe(); > > regards, > Cedric > > 2011/11/16 Evan James Bowling <[email protected] (mailto:[email protected])>: > > On Wed, Nov 16, 2011 at 7:03 AM, Rahul <[email protected] > > (mailto:[email protected])> wrote: > > > > > > hi friends, > > > > > > i went through "Douglas Crockford" book "Javascript : The good parts" > > > > > > and i rearranged the code as below and it worked, > > > > > > > > > (function(){ > > > > > > var personProto = { > > > describe : function(){ > > > alert("NAME IS "+this.name (http://+this.name)); > > > } > > > }; > > > > > > > > > I like the part below, clever :) > > > > > > > > if(typeof Object.createObj !== 'function'){ > > > Object.createObj = function(protoOfObj){ > > > var F = function(){}; > > > F.prototype = protoOfObj; > > > return new F(); > > > }; > > > } > > > > > > var jane = Object.createObj(personProto); > > > jane["name"] = "JANE"; > > > > > > var tarzan = Object.createObj(personProto); > > > tarzan["name"] = "TARZAN"; > > > > > > jane.describe(); > > > tarzan.describe(); > > > > > > })(); > > > > > > > > > Thoughts on it please , > > > > > > waiting for the reply > > > > > > > > > On Nov 16, 1:20 pm, Rahul <[email protected] (http://gmail.com)> > > > wrote: > > > > i removed the line > > > > __proto__ : personProto > > > > > > > > from both the object jane and tarzan, and i added > > > > > > > > Object.getPrototypeOf(jane) = personProto; > > > > Object.getPrototypeOf(tarzan) = personProto; > > > > > > > > but still at line > > > > jane.describe(); > > > > i gives an error that "Object doesn't support this property or method" > > > > > > > > On Nov 16, 12:15 pm, Anoop Gupta <[email protected] > > > > (http://gmail.com)> wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > _prop_ property is deprecated and should not be used in new code: use > > > > > > > > > > Object.getPrototypeOf<https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/...> > > > > > instead. > > > > > Anyways this code is working. > > > > > tried it onhttp://jsfiddle.net/anoop/HfsKC/ > > > > > > > > > > > > > > > > > > -AG > > > > > > > > > On Wed, Nov 16, 2011 at 12:07 PM, Rahul <[email protected] > > > > > (http://gmail.com)> > > > > > wrote: > > > > > > (function(){ > > > > > > > > > > > > > > > > > > > > > > > > var personProto = { > > > > > > describe : function(){ > > > > > > alert("NAME IS "+this.name > > > > > > (http://+this.name)); > > > > > > } > > > > > > }; > > > > > > > > > > > > > > > > > > > > > > > > > var jane = { > > > > > > name : "JANE", > > > > > > __proto__ : personProto > > > > > > }; > > > > > > > > > > > > > > > > > > > > > > > > > var tarzan = { > > > > > > name : "TARZAN", > > > > > > __proto__ : personProto > > > > > > }; > > > > > > > > > > > > > > > > > > > > > > > > > jane.describe(); > > > > > > > > > > })(); > > > > > > > > > > in the above code for the last line i am getting an error "Object > > > > > > doesn't support this property or method", > > > > > > > > > > > > > > > > > > > > > > > > > But what i am expecting is object "jane" must inherit method > > > > > > describe > > > > > > from object "personProto" > > > > > > > > > > > > > > > > > > > > > > > > > can anyone please help me out > > > > > > > > > > -- > > > > > > To view archived discussions from the original JSMentors Mailman > > > > > > list: > > > > > > http://www.mail-archive.com/[email protected]/ > > > > > > > > > > > > > > > > > > > > > > > > > To search via a non-Google archive, visit here: > > > > > > http://www.mail-archive.com/[email protected]/ > > > > > > > > > > > > > > > > > > > > > > > > > To unsubscribe from this group, send email to > > > > > > [email protected] > > > > > > (mailto:[email protected]) > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > Regards > > > > > Anoop K. Gupta > > > > > > > > > > > > > > > > > > > > > > -- > > > To view archived discussions from the original JSMentors Mailman list: > > > http://www.mail-archive.com/[email protected]/ > > > > > > To search via a non-Google archive, visit here: > > > http://www.mail-archive.com/[email protected]/ > > > > > > To unsubscribe from this group, send email to > > > [email protected] > > > (mailto:[email protected]) > > > > > > > > > -- > > To view archived discussions from the original JSMentors Mailman list: > > http://www.mail-archive.com/[email protected]/ > > > > To search via a non-Google archive, visit here: > > http://www.mail-archive.com/[email protected]/ > > > > To unsubscribe from this group, send email to > > [email protected] > > (mailto:[email protected]) > > > > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > (mailto:[email protected]) > > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
