Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #12845
| From | cezary.tomczyk@gmail.com |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Adding new method to existing object |
| Date | 2012-03-17 04:58 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <16476250.1111.1331985481075.JavaMail.geo-discussion-forums@vbiz13> (permalink) |
| References | <31786995.22.1331895413978.JavaMail.geo-discussion-forums@vbat19> <571e5767-fc18-4945-b356-4004af1145e8@q12g2000yqg.googlegroups.com> <10224075.1379.1331918290476.JavaMail.geo-discussion-forums@yniz5> <694b2fe2-7b9f-4e17-87bf-76ce8969d947@p13g2000yqd.googlegroups.com> |
W dniu piątek, 16 marca 2012, 21:01:20 UTC+1 użytkownik David Mark napisał:
> On Mar 16, 1:18 pm, cezary.tomc...@gmail.com wrote:
> > W dniu piątek, 16 marca 2012, 12:50:39 UTC+1 użytkownik David Mark napisał:
> >
> > > On Mar 16, 6:56 am, cezary.tomc...@gmail.com wrote:
> > > > Hi,
> >
> > > > I want to extend my prototype object using another object. Just created some example code:
> >
> > > >http://jsfiddle.net/u9e7R/13/
> >
> > > > I tested the addMethod function in Firefox 10,11, Chrome 17 and IE 9. But it will fail in IE8. In IE8 console when I run jsCode().test2(); I see 'Object does not support property or method "test2"'.
> >
> > > You added it too late to have any effect on the constructed object.
> > > Augmenting the prototype property of the constructor won't do anything
> > > until you construct another object. On the contrary, you hacked the
> > > internal prototype of the constructed object for the other browsers.
> >
> > > What exactly are you trying to accomplish with this?
> >
> > I try to extend my jsCode object by adding another objects to object jsCode.
>
> As mentioned, this is vague.
>
> I yanked out your other (presumably) working forks, which simplifies
> things quite a bit, the problem should jump off the page:-
>
> var global = this;
>
> if (typeof global.jsCode == 'undefined')
> {
> global.jsCode = {};
> }
>
> (function( global, doc )
> {
> var jsCode = function( params )
> {
> return new jsCode.init( params );
> };
>
> jsCode.init = function( params )
> {
> return this;
> };
>
> jsCode.init.prototype =
> {
> constructor: jsCode,
>
> // You stepped on the constructor property. I assume this was
> intentional, but the next method fails to take this into account.
Hm, I belive that I was read somewhere about seeting constructor in prototype. But I see that this is not needed. As I understand constructor returns a reference to the Object function that created the instance's prototype. So, I do not have to set this again in my case. I'm right?
> getPrototypeObject : function( object )
> {
>
> // return object.constructor.prototype
>
> return jsCode.init.prototype;
>
> // See the difference?
Yes. In my case it is enought to return object jsCode.init.prototype;
> },
>
> test : function()
> {
> console.log('Function test complete');
> return this;
> },
>
> addMethod : function (name, fn)
> {
> var object = this.getPrototypeObject( this );
>
> // Save a reference to the old method
> var old = object[ name ];
>
> // Overwrite the method with our new one
> object[ name ] = function()
> {
> // Check the number of incoming arguments,
> // compared to our overloaded function
> if ( fn.length == arguments.length )
> {
> // If there was a match, run the function
> return fn.apply( this, arguments );
> }
> // Otherwise, fallback to the old method
> else if ( typeof old === "function" )
> {
> return old.apply( this, arguments );
> }
> };
> }
>
> };
>
> // Expose jsCode to the global object
> global.jsCode = jsCode;
>
> })( global, document );
>
> var test2 = function()
> {
> console.log('Function test2 complete');
> return this;
> };
>
> jsCode().addMethod('test2', test2);
> console.log('test')
> console.log( jsCode() );
>
> jsCode().test2().test();
>
> > Exactly to jsCode prototype to use inheritance. I just want to create jsCode and implement method chaining. If I described it correctly.
> >
>
> It's going to need some more explanation; it looks unnecessarily
> complicated to me. Why would a constructor function need an "init"
> method? Reminds me of the old Prototypejs library (not a good thing).
I want to create an object jsCode and add then some methods. Like in, for example, PHP where I have classes and can extend them by another methods. In my case the jsCode is a object, have a "bulit-in" methods and can be "extended" by another methods (like a extensions or plugin, somethings line that).
I just want to implement chained method calls and don't want to use new operator every time. That's why I use the new operator and create an instance of a jsCode object. Maybe this way is not good practice.
Finally I want to use every method like this: jsCode().method1().method2().methodX();
Of course, some methods will return this object, some not.
--
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