Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > two methods incompatible ?

Reply
Thread Tools

two methods incompatible ?

 
 
Une Bévue
Guest
Posts: n/a
 
      10-13-2011
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){
for(var i=0,l=this.length;i<l;i++){if(this[i]===s){return true;}}
return false;
}

and the method 'Update' comes from the other pice of code, an M V C
ctriad constructors, to simplify :

function Model()
{
this.observers=new Array();
function Attach( Observer )
{
this.observers.push( Observer );
}
this.Attach=Attach;

.... about the "same" with Detach Observer )

function Notify()
{
for( var i in this.observers )
{
this.observers[i].Update( this );// HERE THE PROB
}
}
this.Notify=Notify;

....
}

the Update method is defined in the View part :
function View( Observable)
{
Observable.Attach( this ); // Observable is a new Model()
function Update( Observable )
{
var id=Observable.state.id;
switch( Observable.state.XHR.readyState )
{
...
}
}
this.Update=Update;

...

}

I don't understand clearly what's the problem...
I understand that this.observers is an Array and i want to had a method
include to the Object Array. The Update method applies to an object in
this array :
this.observers[i].Update( this );

clearly this.observers[i] is a View( model)

may be this comes up because of mixing technics to add methods ?

does it means that i do have to use prototypr also for my constructors :

function Model() {...}
function View( Observable) { ... }
function Controller( Observable ) { ... }

for example for Model#Notify should I use something like :

function Model()
{
this.observers=new Array();
}

that's all, the methods being defined like that :
Model.prototype.Notify = function() {
for( var i in this.observers )
{
this.observers[i].Update( this );
}
}

any light appreciated !


--
« L'éléphant ne peut pas courir et se gratter
les fesses en même temps. »
(Proverbe africain)
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      10-13-2011
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).

> for(var i=0,l=this.length;i<l;i++){if(this[i]===s){return true;}}
> return false;
>
> }
>
> and the method 'Update' comes from the other pice of code, an
> M V C ctriad constructors, to simplify :
>
> function Model()
> {
> this.observers=new Array();
> function Attach( Observer )
> {
> this.observers.push( Observer );
> }
> this.Attach=Attach;
>
> ... about the "same" with Detach Observer )
>
> function Notify()
> {
> 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 -.

> }
> }
> this.Notify=Notify;
>
> ...
>
> }
>
> the Update method is defined in the View part :
> function View( Observable)
> {
> Observable.Attach( this ); // Observable is a new Model()
> function Update( Observable )
> {
> var id=Observable.state.id;
> switch( Observable.state.XHR.readyState )
> {
> ...
> }
> }
> this.Update=Update;
>
> ...
>
> }
>
> I don't understand clearly what's the problem...
> I understand that this.observers is an Array and i want to had
> a method include to the Object Array. The Update method applies
> to an object in this array :
> this.observers[i].Update( this );
>
> clearly this.observers[i] is a View( model)


Not when - i - has the value 'include'.

> may be this comes up because of mixing technics to add methods ?


No, that is not relevant to this issue.

> does it means that i do have to use prototypr also for my
> constructors :
>
> function Model() {...}
> function View( Observable) { ... }
> function Controller( Observable ) { ... }
>
> for example for Model#Notify should I use something like :
>
> function Model()
> {
> this.observers=new Array();
>
> }
>
> 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.
 
Reply With Quote
 
 
 
 
Une Bévue
Guest
Posts: n/a
 
      10-13-2011
Richard Cornford <> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
The Adapter Design Pattern - Relates two otherwise incompatible interfaces priya Java 0 06-26-2006 04:23 PM
The Adapter Design Pattern - Relates two otherwise incompatible interfaces priya Java 0 06-25-2006 01:49 PM
why am i getting incompatible error MNQ VHDL 5 04-16-2004 07:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57