PDA

View Full Version : browser calling a VAR a function



jcdevelopment
Jul 14th, 2008, 08:29 PM
here is my code



this.getEligible = function()
{
this.eligible = new Array();
for (i in this.suggestions)
{
var suggestion = this.suggestions[i];

if(suggestion.indexOf(this.inputText) == "0")
{
this.eligible[this.eligible.length]=suggestion;
}
}
};



The line that states



if(suggestion.indexOf(this.inputText) == "0")
{
this.eligible[this.eligible.length]=suggestion;
}



There seems to be something wrong with it. The browsers say that suggestion.indexOf is not a function.

Well its not, its a variable. Am i doing something wrong?

Thanks for the help guys!

Sorry, here is the error message : suggestion.indexOf is not a function

_Aerospace_Eng_
Jul 14th, 2008, 10:19 PM
Hmm it is a function but it won't ever equal "0". It might equal 0 without the quotes.

Trinithis
Jul 14th, 2008, 10:43 PM
indexOf returns a number, not a string, so there is no need to coerce a string. (The == "0" does work, but there's no point to it.)

Always, always, always declare a variable before using it.



this.getEligible = function()
{
this.eligible = [];
for (var i in this.suggestions)
{
var suggestion = this.suggestions[i];
alert(typeof suggestion);
alert(suggestion);
if(suggestion.indexOf(this.inputText) == 0)
{
this.eligible.push(suggestion);
}
}
};

jcdevelopment
Jul 14th, 2008, 10:49 PM
thanks _Aerospace_Eng_ but unfortunately the root is much deeper than it appears. Im going to try and make this as easy as possible. I hope someone can help me.

the above code has changed to this(the original)


this.getEligible = function()
{
this.eligible = new Array();
for (i in this.suggestions)
{
var suggestion = this.suggestions[i];
alert(suggestion);
if(suggestion.toLowerCase().indexOf(this.inputText.toLowerCase()) == "0") {
this.eligible[this.eligible.length]=suggestion;

}

}
};


The one highlighted in red seems to be the problem.

with the error
Error: suggestion.toLowerCase is not a function
Line: 374
Now when i load my JS file prototype.js is wont work, but when i take it out it does. When i alert the problem it tells me that this code is the problem.



function (iterator, context) {
var index = 0;
iterator = iterator.bind(context);
try {
this._each(function (value) {iterator(value, index++);});
} catch (e) {
if (e != $break) {
throw e;
}
}
return this;
}


which is in my prototype.js file.

When deleted it goes to the next function after that, i am so confused.

So when i take this code out of prototype.js, autosuggest.js then works. Why does this code clash???



var $break = { };

var Enumerable = {
each: function(iterator, context) {
var index = 0;
iterator = iterator.bind(context);
try {
this._each(function(value) {
iterator(value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
},

eachSlice: function(number, iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var index = -number, slices = [], array = this.toArray();
while ((index += number) < array.length)
slices.push(array.slice(index, index+number));
return slices.collect(iterator, context);
},

all: function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var result = true;
this.each(function(value, index) {
result = result && !!iterator(value, index);
if (!result) throw $break;
});
return result;
},

any: function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var result = false;
this.each(function(value, index) {
if (result = !!iterator(value, index))
throw $break;
});
return result;
},

collect: function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var results = [];
this.each(function(value, index) {
results.push(iterator(value, index));
});
return results;
},

detect: function(iterator, context) {
iterator = iterator.bind(context);
var result;
this.each(function(value, index) {
if (iterator(value, index)) {
result = value;
throw $break;
}
});
return result;
},

findAll: function(iterator, context) {
iterator = iterator.bind(context);
var results = [];
this.each(function(value, index) {
if (iterator(value, index))
results.push(value);
});
return results;
},

grep: function(filter, iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var results = [];

if (Object.isString(filter))
filter = new RegExp(filter);

this.each(function(value, index) {
if (filter.match(value))
results.push(iterator(value, index));
});
return results;
},

include: function(object) {
if (Object.isFunction(this.indexOf))
if (this.indexOf(object) != -1) return true;

var found = false;
this.each(function(value) {
if (value == object) {
found = true;
throw $break;
}
});
return found;
},

inGroupsOf: function(number, fillWith) {
fillWith = Object.isUndefined(fillWith) ? null : fillWith;
return this.eachSlice(number, function(slice) {
while(slice.length < number) slice.push(fillWith);
return slice;
});
},


inject: function(memo, iterator, context) {
iterator = iterator.bind(context);
this.each(function(value, index) {
memo = iterator(memo, value, index);
});
return memo;
},

invoke: function(method) {
var args = $A(arguments).slice(1);
return this.map(function(value) {
return value[method].apply(value, args);
});
},

max: function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var result;
this.each(function(value, index) {
value = iterator(value, index);
if (result == null || value >= result)
result = value;
});
return result;
},

min: function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var result;
this.each(function(value, index) {
value = iterator(value, index);
if (result == null || value < result)
result = value;
});
return result;
},

partition: function(iterator, context) {
iterator = iterator ? iterator.bind(context) : Prototype.K;
var trues = [], falses = [];
this.each(function(value, index) {
(iterator(value, index) ?
trues : falses).push(value);
});
return [trues, falses];
},

pluck: function(property) {
var results = [];
this.each(function(value) {
results.push(value[property]);
});
return results;
},

reject: function(iterator, context) {
iterator = iterator.bind(context);
var results = [];
this.each(function(value, index) {
if (!iterator(value, index))
results.push(value);
});
return results;
},

sortBy: function(iterator, context) {
iterator = iterator.bind(context);
return this.map(function(value, index) {
return {value: value, criteria: iterator(value, index)};
}).sort(function(left, right) {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
}).pluck('value');
},

toArray: function() {
return this.map();


Thanks for any help

rnd me
Jul 14th, 2008, 11:29 PM
you only had one simple problem in your original post:

i also added the string coerce to guarantee that indexof would be available.



this.getEligible = function()
{
this.eligible = new Array();
for (i in this.suggestions)
{
if(this.suggestions.hasOwnProperty(i){
var suggestion = String( this.suggestions[i] );

if(suggestion.indexOf(this.inputText) == "0")
{
this.eligible[this.eligible.length]=suggestion;
}
}//end if own prop
}
};

jcdevelopment
Jul 15th, 2008, 03:54 PM
Thanks for all of the help. I figured out the problem with my autosuggest.js and prototype.js.
Trinithis you were the closest to what i needed, i previously had


for (var i in this.suggestions)

that wont work for prototype, but this did


for (var i=0; i < this.suggestions; ++i)

so all is well except my autosuggest.js wont work now, but i will look into that i suppose.

rnd me
Jul 15th, 2008, 05:51 PM
for (var i in this.suggestions)
that wont work for prototype



ot works fine, its part of the ECMA spec, and Prototype doesnt change this. there is a bug in the spec where members of object prototypes show up in for-in loop.

these can be easily enough filtered out however by using hasOwnProperty:

for (i in this.suggestions)
{
if(this.suggestions.hasOwnProperty(i){

this extra step must be used whenever you use a for-in on any object containaing prototype member. a lot of people dont understand this, and they complain when i use prototypes in my posted examples, i "break" their other loops.