Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7331

Re: two methods incompatible ?

Newsgroups comp.lang.javascript
Subject Re: two methods incompatible ?
From unbewusst.sein@fai.invalid (Une Bévue)
Date 2011-10-13 13:41 +0200
Message-ID <1k92w71.a1ntfn14fjrx2N%unbewusst.sein@fai.invalid> (permalink)
References <1k929vd.1qinyf91vxlqgwN%unbewusst.sein@fai.invalid> <6437f80d-fee6-494a-888a-a556be789736@w5g2000yqw.googlegroups.com>
Organization Oedipe Roi

Show all headers | View raw


Richard Cornford <Richard@litotes.demon.co.uk> wrote:

> On Oct 13, 4:58 am, Une Bévue wrote:
> > i have two pices of code allready tested separately but when i
> > want to put them together i get a javascript error (doesn't
> > depend on browser) :
> >
> > Uncaught TypeError: Object function (s){
> >   for(var i=0,l=this.length;i<l;i++){if(this[i]===s){return true;}}
> >   return false;
> >
> > } has no method 'Update'
> >
> > the code printed out comes from :
> > Array.prototype.include=function(s){
> 
> Adding a method to the - Array.prototype - adds a method that is
> inherited by all Array objects created by code within/under the (or
> any) global execution context. However, that added method is
> enumerable ( the - include - property of those Arrays do not have the
> DontEnum - attribute).

yes i knew that point.

> 
> >   for(var i=0,l=this.length;i<l;i++){if(this[i]===s){return true;}}
> >   return false;

<snip />

> >     for( var i in this.observers )
> >     {
> >       this.observers[i].Update( this );// HERE THE PROB
> 
> A - for-in - loop loops over all of the enumerable properties of an
> object. For an Array this will be all of the 'integer-index' property
> names, but also includes any methods added to the - Array.prototype -,
> as they are also enumerable. Your problem is that at this point there
> will be an iteration of the - for-in - loop where - i - has the value
> 'include' and so the value of - this.observers[i] - will be the
> function object that has been assigned to the - Array.prototype -, and
> that function object does not have an - Update - method.
> 
> There are a number of approaches for avoiding this issue, such as
> filtering the properties enumerated in a - for-in - loop (e.g. using
> the - hasOwnProperty - method of objects). However, here the issues
> actually arises because it was inappropriate to use a - for-in - loop
> to loop through the elements of an Array, and instead an ordinary -
> for - loop limited by the array's - length -  should have been used.
> An ordinary - for - loop would only have attempted to access the
> integer index properties and so have been tripped up by enumerable
> methods added to the - Array.prototype -.


that's obvious in fact !!!
sorry for the noise !!!

that's now clear to me, usually i do, for an arrey :
for(var i=0, l=array.length; i < l; i++) {
  // do something...

for objects :

var emptyObject = { };
for(var key in anObject) {
  if(!emptyObject[ key ]) {
    //  do something...

my biggest fault here is "cut'n paste" without carefully reading the
code...

<snip />

> >
> > clearly this.observers[i] is a View( model)
> 
> Not when - i - has the value 'include'.

YES, for sure !!!

> > may be this comes up because of mixing technics to add methods ?
> 
> No, that is not relevant to this issue.
> 

Again, that's obvious to me right now...
... after your reading.

<snip />

> > that's all, the methods being defined like that :
> > Model.prototype.Notify = function() {
> >   for( var i in this.observers )
> >   {
> >     this.observers[i].Update( this );
> >   }
> >
> > }
> 
> You don't have to add method in that way, though if the method will
> allow that (don't care about the context in which they are created)
> then creating methods in that way is both simple and efficient.
> 
> > any light appreciated !
> 
> Richard.

Thanks a lot for your answer, it makes me a bit more clever )))

Yvon

-- 
  « Chez un homme politique, les études c'est quatre ans de droit, 
    puis toute une vie de travers. » 
    (Coluche) 

Back to comp.lang.javascript | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

two methods incompatible ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-13 05:58 +0200
  Re: two methods incompatible ? Richard Cornford <Richard@litotes.demon.co.uk> - 2011-10-13 03:14 -0700
    Re: two methods incompatible ? unbewusst.sein@fai.invalid (Une Bévue) - 2011-10-13 13:41 +0200

csiph-web