Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #12846
| From | cezary.tomczyk@gmail.com |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Adding new method to existing object |
| Date | 2012-03-17 05:17 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <23935524.6.1331986626075.JavaMail.geo-discussion-forums@vbla14> (permalink) |
| References | <31786995.22.1331895413978.JavaMail.geo-discussion-forums@vbat19> <fe0320ca-41db-425e-8be2-dcec3878b903@v2g2000vbx.googlegroups.com> |
W dniu sobota, 17 marca 2012, 04:15:52 UTC+1 użytkownik Scott Sauyet napisał:
> 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?
>
> 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.
>
> 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.
>
> Here is the simplified code, also available at <http://jsfiddle.net/
> u9e7R/18/>
It's simpler, like I've done this before, but not on the jsFiddle. Next I just 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`.
>
> var JsCode = (function() {
> var JsCode = function(params) {
> };
>
> //~ Could use your technique of assigning the prototype,
> //~ but I don't see the point. I just refer to it directly.
> var proto = JsCode.prototype;
>
> proto.test = function() {
> console.log('Function test complete');
> return this;
> };
>
> //~ Since this is added to the prototype, there is no
> //~ need for a function to find the prototype of the object.
> JsCode.addMethod = function (name, fn) {
> var old = proto[name];
>
> proto[name] = function() {
> if ( fn.length == arguments.length ) {
> return fn.apply( this, arguments );
> } else if ( typeof old === "function" ) {
> // really need error checking here.
> return old.apply( this, arguments );
> }
> };
> };
>
> return JsCode;
>
> })();
>
> var test2 = function() {
> console.log('Function test2 complete');
> return this;
> };
>
> JsCode.addMethod('test2', test2);
>
> //~ Without the factory function, you must use `new`.
> var jsc = new JsCode();
> jsc.test2(); //~ or chain these as in the original
> jsc.test();
>
>
> 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:
>
> var result = function(params) {
> return new JsCode(params);
> }
> result.addMethod = JsCode.addMethod;
> return result;
>
> (although that could be further simplified if desired.)
>
> 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.
--
Regards,
CT
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-16 03:56 -0700
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 04:50 -0700
Re: Adding new method to existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-16 15:31 +0100
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 11:51 -0700
Re: Adding new method to existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-17 00:10 +0100
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 17:25 -0700
Re: Adding new method to existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-17 02:04 +0100
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 18:15 -0700
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-16 10:18 -0700
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 11:56 -0700
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 13:01 -0700
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-17 04:58 -0700
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 16:04 -0700
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-18 04:24 -0700
Re: Adding new method to existing object John G Harris <john@nospam.demon.co.uk> - 2012-03-18 13:16 +0000
Re: Adding new method to existing object John G Harris <john@nospam.demon.co.uk> - 2012-03-16 17:09 +0000
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-16 10:46 -0700
Re: Adding new method to existing object Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-16 13:43 -0700
Re: Adding new method to existing object Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-16 20:15 -0700
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-17 05:17 -0700
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-17 05:24 -0700
Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 16:16 -0700
Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-18 04:27 -0700
csiph-web