[Prototype-core] Re: "this" subclasses?
Though it's not like I wished to write it, but it's very interesting to know for other cases. For now I've done it how Mislav Marohnić described. Thank you all PS: sorry again for the wrong list On May 7, 3:57 pm, "Ryan Gahl" <[EMAIL PROTECTED]> wrote: > Couple things (issues explained 1 by 1)... > > First, in your code here you are accessing the add object as a static object > on the class, when in fact it is a prototyped instance member. Meaning you > first need to create an instance of your class to get at .add... > > var my_test_instance = new my_test(); > my_test_instance.add.txt(); > > However... you are also missing an "initialize" function, which is necessary > if you are using Class.create(). So you first have to rewrite your class > like this: > > my_test.prototype = { > textarea: 1, > initialize: function() {}, > add: { > txt: function() { > console.log(this.textarea); > > } > } > } > > Then... like the others have been saying, even with a bind on that txt() > function, the best you'd be doing is mapping "this" to the "add" object > that's been tacked on to the prototype. If you are interested in writing > this class in a manner similar to this, you must use a closure to wrap the > object returned. To do this, turn "add" into a function that returns an > object... (then .bind(this) on the methods of that object will work to map > "this" to your my_test instance), however you must call add as a function. > This pattern _can_ be helpful though under some more complicated scenarios. > For now, it may be close to what you were looking for. > > my_test.prototype = { > textarea: 1, > initialize: function() {}, > add: function () { > return { > txt: function() { > console.log(this.textarea); > }.bind(this)} > > } > }; > > var my_test_instance = new my_test(); > my_test_instance.add().txt(); > > On 5/5/07, Christoph Roeder <[EMAIL PROTECTED]> wrote: > > > > > > > Hello, > > > I hope anybody can give me a hint why this dosn't work? > > > Thanks in advance > > > > > var my_test = Class.create(); > > my_test.prototype = { > > textarea: 1, > > > add: { > > txt: function(){ > > console.log(this.textarea); > > } > > } > > } > > > my_test.add.txt(); > > -- > Ryan Gahl > Software Architect > WebWidgetry.com / MashupStudio.com > Future Home of the World's First Complete Web Platform > Inquire: 1-262-951-6727 > Blog:http://www.someElement.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
And also, as Mislav said, try to keep these types of questions limited to the ruby spinoffs list - or some other general javascript help list. (Sorry to perpetuate the thread on this list guys... just thought he could use a bit more help) On 5/7/07, Ryan Gahl <[EMAIL PROTECTED]> wrote: > > Couple things (issues explained 1 by 1)... > > First, in your code here you are accessing the add object as a static > object on the class, when in fact it is a prototyped instance member. > Meaning you first need to create an instance of your class to get at .add... > > > var my_test_instance = new my_test(); > my_test_instance.add.txt(); > > However... you are also missing an "initialize" function, which is > necessary if you are using Class.create(). So you first have to rewrite > your class like this: > > my_test.prototype = { > textarea: 1, > initialize: function() {}, > add: { > txt: function() { > console.log(this.textarea ); > } > } > } > > Then... like the others have been saying, even with a bind on that txt() > function, the best you'd be doing is mapping "this" to the "add" object > that's been tacked on to the prototype. If you are interested in writing > this class in a manner similar to this, you must use a closure to wrap the > object returned. To do this, turn "add" into a function that returns an > object... (then .bind(this) on the methods of that object will work to map > "this" to your my_test instance), however you must call add as a function. > This pattern _can_ be helpful though under some more complicated scenarios. > For now, it may be close to what you were looking for. > > my_test.prototype = { > textarea: 1, > initialize: function() {}, > add: function () { > return { > txt: function() { > console.log(this.textarea); > }.bind(this) > } > } > }; > > var my_test_instance = new my_test(); > my_test_instance.add().txt(); > > > > On 5/5/07, Christoph Roeder < [EMAIL PROTECTED]> wrote: > > > > > > Hello, > > > > I hope anybody can give me a hint why this dosn't work? > > > > Thanks in advance > > > > > > var my_test = Class.create(); > > my_test.prototype = { > > textarea: 1, > > > > add: { > > txt: function(){ > > console.log(this.textarea); > > } > > } > > } > > > > my_test.add.txt(); > > > > > > > > > > > > > -- > Ryan Gahl > Software Architect > WebWidgetry.com / MashupStudio.com > Future Home of the World's First Complete Web Platform > Inquire: 1-262-951-6727 > Blog: http://www.someElement.com -- Ryan Gahl Software Architect WebWidgetry.com / MashupStudio.com Future Home of the World's First Complete Web Platform Inquire: 1-262-951-6727 Blog: http://www.someElement.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
Couple things (issues explained 1 by 1)... First, in your code here you are accessing the add object as a static object on the class, when in fact it is a prototyped instance member. Meaning you first need to create an instance of your class to get at .add... var my_test_instance = new my_test(); my_test_instance.add.txt(); However... you are also missing an "initialize" function, which is necessary if you are using Class.create(). So you first have to rewrite your class like this: my_test.prototype = { textarea: 1, initialize: function() {}, add: { txt: function() { console.log(this.textarea); } } } Then... like the others have been saying, even with a bind on that txt() function, the best you'd be doing is mapping "this" to the "add" object that's been tacked on to the prototype. If you are interested in writing this class in a manner similar to this, you must use a closure to wrap the object returned. To do this, turn "add" into a function that returns an object... (then .bind(this) on the methods of that object will work to map "this" to your my_test instance), however you must call add as a function. This pattern _can_ be helpful though under some more complicated scenarios. For now, it may be close to what you were looking for. my_test.prototype = { textarea: 1, initialize: function() {}, add: function () { return { txt: function() { console.log(this.textarea); }.bind(this) } } }; var my_test_instance = new my_test(); my_test_instance.add().txt(); On 5/5/07, Christoph Roeder <[EMAIL PROTECTED]> wrote: > > > Hello, > > I hope anybody can give me a hint why this dosn't work? > > Thanks in advance > > > var my_test = Class.create(); > my_test.prototype = { > textarea: 1, > > add: { > txt: function(){ > console.log(this.textarea); > } > } > } > > my_test.add.txt(); > > > > > -- Ryan Gahl Software Architect WebWidgetry.com / MashupStudio.com Future Home of the World's First Complete Web Platform Inquire: 1-262-951-6727 Blog: http://www.someElement.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
Thank you, very interesting site. On May 6, 4:45 pm, kangax <[EMAIL PROTECTED]> wrote: > You can start with some of Doug Crockford's must-read articles > athttp://javascript.crockford.com/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
You can start with some of Doug Crockford's must-read articles at http://javascript.crockford.com/ On May 6, 6:07 am, Christoph Roeder <[EMAIL PROTECTED]> wrote: > On May 6, 11:58 am, "Mislav Marohnić" <[EMAIL PROTECTED]> > wrote: > > > I suggest you drop these complex constructs (add.txt.foo.bar) until you > > learn more about JavaScript. > > Can you please give me some keywords or links to sites with such > "advanced" topics? > > Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
On May 6, 11:58 am, "Mislav Marohnić" <[EMAIL PROTECTED]> wrote: > I suggest you drop these complex constructs (add.txt.foo.bar) until you > learn more about JavaScript. Can you please give me some keywords or links to sites with such "advanced" topics? Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
On 5/6/07, Christoph Roeder <[EMAIL PROTECTED]> wrote: > > > Is this what I try possible, maybe with Prototypes "bind"? You can only bind to one specific object. Since your methods are instance methods of some class they need to refer to the current object instance with the "this" keyword, and there are potentially many, many instances. So, no bind is possible in this case. I suggest you drop these complex constructs (add.txt.foo.bar) until you learn more about JavaScript. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
Thanks, but one last question: Is this what I try possible, maybe with Prototypes "bind"? On May 6, 12:33 am, "Mislav Marohnić" <[EMAIL PROTECTED]> wrote: > On 5/6/07, Christoph Roeder <[EMAIL PROTECTED]> wrote: > > > > > add: { > > txt: function(){ > > console.log(this.textarea); > > } > > } > > "this" keyword inside the txt() function refers to the object contained in > "add", not the my_test class like you wanted. Try this instead: > > addTxt: function(){ > console.log(this.textarea); > > } > > Anyway, this is not a JavaScript support group - it's only for Prototype > development discussion. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: "this" subclasses?
On 5/6/07, Christoph Roeder <[EMAIL PROTECTED]> wrote: > > > add: { > txt: function(){ > console.log(this.textarea); > } > } > "this" keyword inside the txt() function refers to the object contained in "add", not the my_test class like you wanted. Try this instead: addTxt: function(){ console.log(this.textarea); } Anyway, this is not a JavaScript support group - it's only for Prototype development discussion. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---