By the way, Object.create isn't supported by a number of browsers (IE 8, FF 3.5/3.6, and others).
~Ryan Bales On Tue, Oct 25, 2011 at 7:25 AM, Jake Verbaten <[email protected]> wrote: > > > On Tue, Oct 25, 2011 at 12:59 PM, Scott Sauyet <[email protected]>wrote: > >> Lasse Reichstein wrote: >> > Bemi Faison wrote: >> >> > When you write "new MyClass()", you create and initialize an instance >> > of the abstract type. This is an object of a categorically different >> kind than >> > the constructor/prototype. When you start using that object as a >> prototype >> > of another constructor, you are mixing abstraction levels. That's why >> > people oppose doing "SubConstructor.prototype = new SuperConstructor". >> >> > The properties of a MyClass *instance* doesn't necessarily make sense >> > on the subclass, and they don't make sense when the same property is >> > inherited by all subclass instances. >> >> ... which is why if you do use a constructor in this fashion, you >> should always separate out the initialization into a separately-called >> function, one which is not called in this inheritance scenario. >> > > No. The constructor does initialization logic. if you want an object to be > instantiated and initialized you either do > > new constructor(...); > > or > > var o = Object.create(constructor.prototype); > o.constructor(...); > > The latter splits instantiation and initiliazation. If you do not want the > object to be initialized then you simply don't use new and just instantiate > it using Object.create > > >> >> > When you write "Object.create(MyClass.prototype)", you just create an >> > object inheriting from the prototype. It's not initialized by the >> > MyClass constructor, it just inherits the methods of the abstract >> > MyClass type. >> > >> > Using that as the prototype of a new type keeps the abstraction level. >> >> I simply have never been quite certain when I can depend upon >> Object.create being present and functioning correctly. And I'm wary >> of shims which can never do what the original is supposed to do. >> Which is why I still mostly use: >> >> var F = function() {}; >> F.prototype = Parent.prototype; >> Child.prototype = new F(); >> >> This is perhaps less obvious than Object.create, but it's a well-known >> pattern, and I'm quite certain that the environments I care about >> support it., especially as my current project has to support IE7, and >> I would like it to support older versions of FF as well. >> > > ES5 shim <https://github.com/kriskowal/es5-shim> will shim out a sensible > subset of ES5. > > For example object.create(proto) works completely. Object.create(proto, > pds) works for some subset of pds. > > Of course Object.defineProperty is iffy on support but we are not > suggesting you use. The only thing that really doesnt work at all in IE<9 is > getter/setters and enumerable/configurable/writable along with Object.freeze > and company. > > however we are suggesting you use Object.create(proto) not > Object.create(proto, pd) the former works completely cross browser. > > >> >> I know there are many `Object.create` shims out there, but some of the >> properties that can be defined seem impossible to shim. Would there >> be any way to shim `ennumerable`? >> >> -- Scott >> >> -- >> 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] > -- Ryan Bales http://thinktnk.com/ http://twitter.com/#!/thinkt4nk https://github.com/thinkt4nk -- 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]
