Sent from my iPad
On Oct 27, 2011, at 15:00, "Fyodorov "bga" Alexander" <[email protected]> wrote: > > > ydaniv: >> @Jake: >> No mate, seriously, it's problematic and you're missing it. >> Good design won't solve this. You can't just make sure by design that >> one level down will always bring you a different method when just >> calling Object.getPrototypeOf. >> >> @Alexander: >> First, __defineGetter__ & const aren't standard, so I rather not use >> them (while ES5 is standard but not fully supported there's still a >> difference). >> Second, the idea of looping through the object on every super call to >> get the identifier occurred to me and I ignored it from obvious >> performance reasons. >> Third, hard-coding of getting the property from 2 levels down is just >> a bad idea. >> >> Thanks, >> ~Y >> >> >> On Oct 27, 12:25 am, "Fyodorov "bga" Alexander" >> <[email protected]> wrote: >>> ydaniv: >>> >>> >>> >>> >>> >>> >>> >>> >>> >>>> @Jake: >>>> Yes, I understood exactly why you wanted it configurable, it makes >>>> good sense. >>> >>>> As for the reason we need something more then just >>>> Object.getPrototypeOf(): >>>> I wished it could get down to that and we could simply use this, but, >>>> I'll give you a very common use case where this fails: >>> >>>> var instance = { >>>> __proto__: { >>>> method: function () { alert('a'); }, >>>> __proto__: { >>>> method: function () { alert('super a'); } >>>> } >>>> } >>>> } >>> >>>> "instance" is an instance of some class that has a method "method" and >>>> it inherits another class that has a method by the same name. >>>> If you call Object.getPrototypeOf() on "instance" and ask for the >>>> method "method" you'll get the same method. Bummer! >>>> Say you had this method call its super inside it: >>> >>>> method: function () { alert('a'); return >>>> Object.getPrototypeOf(this).method(); }, >>> >>>> You'll crash the browser. Uber bummer! >>>> So, we need something a bit more comprehensive than that. >>> >>>> As for your pd.make, yes, it's pretty much the same as Create. >>>> Create also lets you cache the resulted object in a function that will >>>> generate the same instance with every call, if you find it necessary. >>>> Like define once and construct multiple times. >>> >>>> @Alexander: >>>> You're right, usually you wouldn't call an overridden property from a >>>> different one. >>>> The thing is, for now, this looks like the smallest, safest, most >>>> simple and least obtrusive way of getting a property. >>>> Besides naming a function and getting it from arguments.callee.caller, >>>> is there another way you see of getting that property? >>> >>>> Thanks a lot guys! >>>> ~Y >>> >>>> On Oct 25, 11:35 am, "Fyodorov "bga" Alexander" >>>> <[email protected]> wrote: >>>>> ydaniv: >>> >>>>>> Thanks guys for the input! >>> >>>>>> @Jake: >>>>>> It's evil to extend Object.prototype with an enumerable property. >>>>>> here "_prototype_" is non-configurable because the logic here depends >>>>>> on it. If you choose to use this then you'd want it to be non- >>>>>> configurable. >>>>>> But perhaps you're right and I could add "configurable: true", seems >>>>>> like a good idea. >>> >>>>>> The "_asDescriptor" function is private and quite straight-forward. >>>>>> The use of the letters e, p &r is a physical joke, and I didn't think >>>>>> that following a single-line function would be too hard to follow. >>>>>> Anyhow, I guess I'll change those to something more meaningful. >>> >>>>>> I'm sorry that you see the supr function as magic, cause actually it >>>>>> follows the straight forward logic of getting an overridden property >>>>>> in a prototype chain: >>>>>> You find the object in the chain that actually defines this property >>>>>> and then get this property again from its parent. >>>>>> Since JS don't protect you from possible errors you need to go through >>>>>> some hoops. >>> >>>>>> What are Object.make & Object.createSimple? >>>>>> And yes, Create is very thin and light, that was the objective here. >>> >>>>>> @Alexander: >>>>>> As I mentioned in the docs, if you name each method you're using supr >>>>>> inside and not in strict mode then you can loose the "name" param. >>> >>>>>> Personally I don't like Resig's this._super because I find it very >>>>>> obtrusive, it bloats your code, and frankly it uses magic like using >>>>>> regex on the function.toString(). >>> >>>>>> If you consider using a function's name inside it as breaking the DRY >>>>>> principle then I guess that's your opinion. In python, you call a >>>>>> super function like this: >>>>>> class C(B): >>>>>> def method(self, arg): >>>>>> super(C, self).method(arg) >>> >>>>>> You could say I took some inspiration from that. >>>>>> Besides that, putting a name of a different property allows you to >>>>>> call a different overridden property, which is not RY at all. >>> >>>>>> Thanks! >>>>>> ~Y >>> >>>>>> On Oct 24, 1:01 pm, "Fyodorov "bga" Alexander" >>>>>> <[email protected]> wrote: >>>>>>> ydaniv: >>> >>>>>>>> Hi, >>> >>>>>>>> I saw some discussions about OO & inheritance in JS so thought I might >>>>>>>> share a small Gist I've contrived: >>> >>>>>>>> https://gist.github.com/1074335 >>> >>>>>>>> It's ES5 only and implements a simple wrap for the Object.create >>>>>>>> mechanism and a "super" property getter in a more unobtrusive way. >>> >>>>>>>> It's OS and will appreciate any feedback. >>> >>>>>>>> Thanks, >>>>>>>> ~Y >>> >>>>>>> this._super() is better then your method because you force me >>>>>>> duplicate method name, its not DRY >>> >>>>> ok, its flexible but if you design/decomposition require call some >>>>> random method from super classes, not exactly what you overwrite - its >>>>> mean that you have bad design or i dont know real use cases >>> >>> Object.prototype.__defineGetter__('_super', function(){ >>> const _caller = arguments.callee.caller >>> const callerName = _caller.jbName || (_caller.jbName = (function(){ >>> for(var k in this) >>> if(this[k] == _caller) >>> return k >>> }.bind(this))()) >>> return Object.getPrototypeOf(Object.getPrototypeOf(this)) >>> [callerName] >>> >>> }) >>> >>> const A = function(){} >>> A.prototype._say = function(){ return '1' } >>> const B = function(){} >>> B.prototype = Object.create(A.prototype) >>> B.prototype.constructor = B >>> B.prototype._say = function(){ return '2' + this._super() } >>> _log((new B())._say()) // '21' > > oh. >_<. Not each time. _caller.jbName || (_caller.jbName = ...). Do > you see? Its cache. Sorry you acts nitpicky. You always can replace > const to var and __defineGetter__ to Object.defineProperty yourself > > -- > 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] -- 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]
