two methods incompatible ?

U

Une Bévue

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===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===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.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.Update( this );

clearly this.observers 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.Update( this );
}
}

any light appreciated !
 
R

Richard Cornford

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===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===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.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 - 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.Update( this );

clearly this.observers 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.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.
 
U

Une Bévue

Richard Cornford said:
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===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===s){return true;}}
return false;

for( var i in this.observers )
{
this.observers.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 - 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...

clearly this.observers is a View( model)


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


YES, for sure !!!
No, that is not relevant to this issue.

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

that's all, the methods being defined like that :
Model.prototype.Notify = function() {
for( var i in this.observers )
{
this.observers.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top