2011/5/17 Jarek Foksa <ja...@kiwi-themes.com>: >> Well, you could always just use Array.prototype. > > I'm sorry, I have formulated my question incorrectly. The question > should rather be: How do I modify Array.prototype.b.c() in the code > below so that "this" would return ['one', 'two', 'three']: > > var myArray = ['one', 'two', 'three']; > > Array.prototype.b = {}; > Array.prototype.b.c = function() { > console.log(this); > } > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com >
Since the array is not yet initialized in the prototype, you cannot reference it. And as you may know, the calling style of the function defines the value of this. So in case: myArray.b.c = myArray.a; this in myArray.b.c will still reference to myArray.b; >From the prototype you cannot really reference the parent object (from Array.prototype.b you cannot reference the array, that will be initialed when the constructor is called.). Maybe choose a different approach, and most likely do not change the Array prototype. A different approach would be to set up those extra functions with a function call. One approach would be: Array.prototype.b = function () { var array = this, b; if (!array.b.c) { b = { c: function () { console.log(array); return array; } }; array.b = b; return b; } }; myArray.b(); myArray.b.c(); The other approach would be: Object.defineProperty(Array.prototype, 'b', { get: function () { var array = this; array.c = function () { console.log(array); return array; } return array; }, set: function () {}, enumerable: false }); myArray.b.c(); -- Poetro -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com