SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: ie and object.prototype
-
Apr 27, 2007, 00:35 #1
- Join Date
- Apr 2007
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ie and object.prototype
Hello
i am trying to use the following code.
it work perfectly with firefox, konqueror, opera, but not with ie (foo.length = 0 despite i add some object).
Does anybody know a solution for this issue ?
Many thanks
--- cut
listeCommandes.prototype = new Array;
function listeCommandes(){
this.ajouter = function (obj){
this.push(obj);
}
this.supprimer = function (index){
this.splice(index,1);
}
this.totalht = 0;
this.totalttc = 0;
this.totaltva = 0;
}
var foo = new listeCommandes()
foo.push("ee");
foo.ajouter("ee");
alert(foo.length);
--- cut
-
Apr 27, 2007, 01:43 #2
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:function listeCommandes() { this.ajouter = function(obj) { this.push(obj); } this.supprimer = function(index) { this.splice(index, 1); } this.totalht = 0; this.totalttc = 0; this.totaltva = 0; } listeCommandes.prototype = Array.prototype; var foo = new listeCommandes(); foo.push("ee"); foo.ajouter("ee"); alert(foo.length);
Birnam wood is come to Dunsinane
-
Apr 27, 2007, 01:52 #3
- Join Date
- Apr 2007
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
on ie, this alert 0.
i explain the problem by to way:
- on the first hand, this.length does seems to work very well.
i 'm trying to make a function like this
this.longueur = function (){
if (this.length){
return this.length
}
else {
for (x=0;this[x];x++){
}
return x;
}
- on the second hand, i think push() and push() alias (ajouter()) also doesn't work very good
i use the following code for test:
foo.push("eeee");
alert(foo[0]);
alert(foo.length);
alert(foo.longueur());
foo.push("eee");
alert(foo[1]);
alert(foo.length);
alert(foo.longueur());
foo.push("ee");
alert(foo[2]);
alert(foo.length);
alert(foo.longueur());
on ie
- alert(foo[1]); alert undefined
- and alert(foo[2]) also
-
Apr 27, 2007, 06:24 #4
- Join Date
- Apr 2007
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This simplier and quicker code that i use for testing may be usefull:
tab.prototype = new Array();
function tab(){
}
var letab = new tab()
alert(letab.push("eeeeeeee")); // push return length witch is equal to 1
alert(letab[0]);
alert(letab.length); // now length is equal to 0 !!!
loving internet explorer....
-
Apr 27, 2007, 07:23 #5
- Join Date
- Aug 2006
- Posts
- 266
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:<script type="text/javascript"> var foo = []; // or var foo = new Array(); alert(foo.length); // 0 foo.push("eeee"); alert(foo[0]); // eeee alert(foo.length); // 1 alert(foo); // eeee foo.push("eee"); alert(foo[1]); // eee alert(foo.length); // 2 alert(foo); // eeee,eee foo.push("ee"); alert(foo[2]); // ee alert(foo.length); // 3 alert(foo); // eeee,eee,ee </script>
Last edited by muazzez; Apr 27, 2007 at 13:22.
-
Apr 27, 2007, 08:12 #6
- Join Date
- Apr 2007
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It seems to work.
and it work also if i add this code for having functions i my object:
foo.bar = function (){
alert('bar');
}
that 's not the way i wish to find but many thanks, i don't find a best solution
( was reading http://tobielangel.com/2007/2/8/javascript-subclassing and http://dean.edwards.name/weblog/2006/11/hooray/ )
Many many thanks another time ! (i losed lot of time while searching a solution)
-
Apr 29, 2007, 13:00 #7
- Join Date
- Feb 2004
- Location
- Third Stone From The Sun
- Posts
- 82
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Array is a constructor for an array. [ ] is an instance of an Array.
gebura - You'll probably have to create your own collections when you need them. You might keep an internal array as a property.
Code:ListeCommandes = function ListeCommandes( type ) { this.type = type. this._items = [ ]; }; ListeCommandes.prototype = { ,addCommands : function( commandArray ) { // might need a typeCheck here. this._items = this._items.concat( commandArray ); } ,getItems : function() { return this._items.concat(); // Defensive copy; see Bloch. } ... };
Code:YAHOO.extend( com.acme.ListeCommands, com.acme.List, { // ListeCommands' List override methods here. });
My outdated site is down for a while now.
Bookmarks