Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: cezary.tomczyk@gmail.com Newsgroups: comp.lang.javascript Subject: Re: Adding new method to existing object Date: Sat, 17 Mar 2012 05:17:06 -0700 (PDT) Organization: http://groups.google.com Lines: 98 Message-ID: <23935524.6.1331986626075.JavaMail.geo-discussion-forums@vbla14> References: <31786995.22.1331895413978.JavaMail.geo-discussion-forums@vbat19> NNTP-Posting-Host: 79.175.205.52 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1331986628 30867 127.0.0.1 (17 Mar 2012 12:17:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 17 Mar 2012 12:17:08 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=79.175.205.52; posting-account=fqiM0AoAAACu3aIqzAK2xf9RRpymvKVH User-Agent: G2/1.0 Xref: csiph.com comp.lang.javascript:12846 W dniu sobota, 17 marca 2012, 04:15:52 UTC+1 u=C5=BCytkownik Scott Sauyet n= apisa=C5=82: > CT wrote: > > I want to extend my prototype object using another object. Just created= some example code: > > > > http://jsfiddle.net/u9e7R/13/ > > > > Can someone explain me how can I extend existing prototype object using= new object? >=20 > David Mark pointed out the basic flaw in your code, that you munged > the constructor. I posted a fairly minimal fix to that earlier. (I > apologize for posting only a link; I know that's poor citizenship on > USENET. I was in a hurry, but that's no excuse.) But that version is > to me still fundamentally flawed. It's far too complicated for what > it does. Below is another version with nearly the same behavior, but > significantly simpler code. >=20 > There are two main differences in behavior: first, there is no factory > function, just the plain constructor; and second, the addMethod > function is attached to the constructor, not to the instances. To me > that simply makes sense, since it is updating the prototype. If you > meant this to apply to instances, the code would be far more simple. >=20 > Here is the simplified code, also available at u9e7R/18/> It's simpler, like I've done this before, but not on the jsFiddle. Next I j= ust wanted to return an instance of an object. > //~ I just attach this to the global namespace here instead > //~ of using the `global` variable to reference `this`. >=20 > var JsCode =3D (function() { > var JsCode =3D function(params) { > }; >=20 > //~ Could use your technique of assigning the prototype, > //~ but I don't see the point. I just refer to it directly. > var proto =3D JsCode.prototype; >=20 > proto.test =3D function() { > console.log('Function test complete'); > return this; > }; >=20 > //~ Since this is added to the prototype, there is no > //~ need for a function to find the prototype of the object. > JsCode.addMethod =3D function (name, fn) { > var old =3D proto[name]; >=20 > proto[name] =3D function() { > if ( fn.length =3D=3D arguments.length ) { > return fn.apply( this, arguments ); > } else if ( typeof old =3D=3D=3D "function" ) { > // really need error checking here. > return old.apply( this, arguments ); > } > }; > }; >=20 > return JsCode; >=20 > })(); >=20 > var test2 =3D function() { > console.log('Function test2 complete'); > return this; > }; >=20 > JsCode.addMethod('test2', test2); >=20 > //~ Without the factory function, you must use `new`. > var jsc =3D new JsCode(); > jsc.test2(); //~ or chain these as in the original > jsc.test(); >=20 >=20 > This does change the interface a little, but seems much cleaner. If > you really want a factory function, you could replace the `return` > statement above with something like this: >=20 > var result =3D function(params) { > return new JsCode(params); > } > result.addMethod =3D JsCode.addMethod; > return result; >=20 > (although that could be further simplified if desired.) >=20 > Does this capture the essence of what you're trying to do? Yes! Thank You for clarifying me. This is exactly what I wanted to achieve. --=20 Regards, CT