Connecting Tech Pros Worldwide Forums | Help | Site Map

alerting all the protype functions

pinti
Guest
 
Posts: n/a
#1: Mar 12 '06
i defined some prototype functions like,

Array.prototype.in_array = function (value) {
for (var hoba in this)
if (this[hoba] == value) return true;
return false;
}

and put them in a separate file and included like,

<script src="commonScripts.js" type="text/javascript"></script>

and there's this script in the file it's included:

str = 'this i re[4], Re[5], fw[100] ve Fw[200]';
re = /(re|fw)\[\d+\]/ig;
nevar = re.exec(str);
ver = '\n\n';
for (var ww in nevar)
ver += ww + ': ' + nevar[ww] + '\n';

alert(ver);

this script alerts all (and only) the prototype functions in the js file
(there are other fynctions and var definitions in it. this is the output:

mukerrer: function () {
var muker = new Array();

for (var bir = 0; bir < this.length-1; bir++) {
for (var iki = bir+1; iki < this.length; iki++) {
if (this[bir] == this[iki]) {
muker[muker.length] = this[bir];
break;
}
}
}
return muker;
}
array_unique: function () {
var unik = new Array();

for (var bir = 0; bir < this.length; bir++) {
yok = true;
for (var iki = 0; iki < unik.length; iki++) {
if (this[bir] == unik[iki]) {
yok = false;
break;
}
}
if (yok)
unik[unik.length] = this[bir];
}
return unik;
}
array_search: function (value) {
for (var loba in this)
if (this[loba] == value) return loba;
return false;
}
in_array: function (value) {
for (var hoba in this)
if (this[hoba] == value) return true;
return false;
}
input: this i re[4], Re[5], fw[100] ve Fw[200]
index: 7
lastIndex: 12
0: re[4]
1: re

how can i solve this problem?





Jonas Raoni
Guest
 
Posts: n/a
#2: Mar 12 '06

re: alerting all the protype functions


pinti wrote:[color=blue]
> i defined some prototype functions like,
>
> Array.prototype.in_array
>
> str = 'this i re[4], Re[5], fw[100] ve Fw[200]';
> re = /(re|fw)\[\d+\]/ig;
> nevar = re.exec(str);
> ver = '\n\n';
> for (var ww in nevar)
> ver += ww + ': ' + nevar[ww] + '\n';[/color]

Do not prototype the Array nor the Object, by doing it you damage the
"for..in" loop, look:

var x = [0, 1], i, s;

for(i in s = "", x)
s += i + "=" + x[i] + "\n";
alert(s);

Object.prototype.OBJECT = Array.prototype.ARRAY = 0;

for(i in s = "", x)
s += i + "=" + x[i] + "\n";
alert(s);

for(i in {})
alert(i);
[color=blue]
>
> alert(ver);
>
> this script alerts all (and only) the prototype functions in the js file
> (there are other fynctions and var definitions in it. this is the output:[/color]


You should put your functions inside a "namespace" to access them this way.

var x = {
variable: 1,
func: function(){}
};

for(var i in x)
alert(i + " = " + x[i]);


--
Now with alcohol <URL:http://youtube.com/watch?v=lnQTZxqxc10> =X
Jonas Raoni Soares Silva
http://www.jsfromhell.com
pinti
Guest
 
Posts: n/a
#3: Mar 12 '06

re: alerting all the protype functions


"Jonas Raoni" <jonasraoni@gmail.com>, haber iletisinde sunlari yazdi:dv1hlg$ug5$1@emma.aioe.org...[color=blue]
> pinti wrote:[color=green]
>> i defined some prototype functions like,
>>
>> Array.prototype.in_array
> >
>> str = 'this i re[4], Re[5], fw[100] ve Fw[200]';
>> re = /(re|fw)\[\d+\]/ig;
>> nevar = re.exec(str);
>> ver = '\n\n';
>> for (var ww in nevar)
>> ver += ww + ': ' + nevar[ww] + '\n';[/color]
>
> Do not prototype the Array nor the Object, by doing it you damage the "for..in" loop, look:
>
> var x = [0, 1], i, s;
>
> for(i in s = "", x)
> s += i + "=" + x[i] + "\n";
> alert(s);
>
> Object.prototype.OBJECT = Array.prototype.ARRAY = 0;
>
> for(i in s = "", x)
> s += i + "=" + x[i] + "\n";
> alert(s);
>
> for(i in {})
> alert(i);
>[color=green]
>>
>> alert(ver);
>>
>> this script alerts all (and only) the prototype functions in the js file (there are other
>> fynctions and var definitions in it. this is the output:[/color]
>
>
> You should put your functions inside a "namespace" to access them this way.
>
> var x = {
> variable: 1,
> func: function(){}
> };
>
> for(var i in x)
> alert(i + " = " + x[i]);
>
>[/color]

sorry, i didn't get it.

i define a in_array method for Array (as in php):

Array.prototype.in_array = function (value) {
for (var hoba in this)
if (this[hoba] == value) return true;
return false;
}

and use it like:
items = ['a', 'b', 'c'];
item = 'c';
if (items.in_array(item))
alert('it is!');

this gives me what i want. on the other hand, a code that has nothing to do with the prototype such
as:

oitems = ['j', 'k', 'l'];
s = '';
for (var i in oitems)
s += i + ': ' + oitems[i] + '\n';
alert(s);

gives:

in_array: function (value) {
for (var hoba in this)
if (this[hoba] == value) return true;
return false;
}
0: j
1: k
2: l

is it because i use for (i in ...) for arrays whereas it must be used for objects (in this case, why
doesn't if list all the predefined or built-in Array methods?)
or is it that i prototype wrongly? or what?


Michael Winter
Guest
 
Posts: n/a
#4: Mar 12 '06

re: alerting all the protype functions


On 12/03/2006 19:48, pinti wrote:
[color=blue]
> "Jonas Raoni" [wrote]:[/color]

[snip]
[color=blue][color=green]
>> Do not prototype the Array nor the Object,[/color][/color]

If adding a method or property to the prototype object of either is the
best solution, then do it.
[color=blue][color=green]
>> by doing it you damage the "for..in" loop, [...][/color][/color]

That statement is so rarely needed that it's hardly an argument.
Besides, if all that's being added is functions then those additions can
be screened out.

[snip]
[color=blue]
> Array.prototype.in_array = function (value) {
> for (var hoba in this)
> if (this[hoba] == value) return true;
> return false;
> }[/color]

Unless you expect your arrays to be large and sparse, use a 'regular'
for statement and loop through the numeric properties bounded by the
length property.

[snip]
[color=blue]
> (in this case, why doesn't if list all the predefined or built-in
> Array methods?)[/color]

The vast majority (perhaps all, I'm not going to check) of specified
built-in methods (of all objects) are defined with the DontEnum
attribute. This is a specification mechanism unavailable to user-defined
code that prevents these methods from being enumerated using a for..in
statement.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jonas Raoni
Guest
 
Posts: n/a
#5: Mar 12 '06

re: alerting all the protype functions


Michael Winter wrote:[color=blue]
> On 12/03/2006 19:48, pinti wrote:[color=green]
>> "Jonas Raoni" [wrote]:[color=darkred]
>>> Do not prototype the Array nor the Object,[/color][/color]
>
> If adding a method or property to the prototype object of either is the
> best solution, then do it.[/color]

I don't see a good reason to to this. Sure it looks prettier, but in my
opinion it's a high price for damaging the language =/
[color=blue][color=green][color=darkred]
>>> by doing it you damage the "for..in" loop, [...][/color][/color]
>
> That statement is so rarely needed that it's hardly an argument.
> Besides, if all that's being added is functions then those additions can
> be screened out.[/color]

It doesn't matter if it's not used often, there are a lot of things that
I don't use often, but this isn't an excuse to kill it. If you can avoid
breaking the language by just passing an extra argument, why not?

If someone add new code that use such loops, they will break because you
were worried about prettifying the code =]

[color=blue][color=green]
>> Array.prototype.in_array = function (value) {
>> for (var hoba in this)
>> if (this[hoba] == value) return true;[/color][/color]

I think the "===" would be a better operator.
[color=blue]
> Unless you expect your arrays to be large and sparse, use a 'regular'
> for statement and loop through the numeric properties bounded by the
> length property.[/color]

I agree, the for..in loop is quite fast under Flash, but on JavaScript
it's slow and useful only for cycling through object properties.
[color=blue][color=green]
>> (in this case, why doesn't if list all the predefined or built-in
>> Array methods?)[/color]
>
> The vast majority (perhaps all, I'm not going to check) of specified
> built-in methods (of all objects) are defined with the DontEnum
> attribute. This is a specification mechanism unavailable to user-defined
> code that prevents these methods from being enumerated using a for..in
> statement.[/color]

Yeah =]

alert([].propertyIsEnumerable("join"));


--
Now with alcohol <URL:http://youtube.com/watch?v=lnQTZxqxc10> =X
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Mar 13 '06

re: alerting all the protype functions


pinti wrote:
[color=blue]
> i defined some prototype functions like,
>
> Array.prototype.in_array = function (value) {
> for (var hoba in this)
> if (this[hoba] == value) return true;
> return false;
> }[/color]

This will iterate through all enumerable properties of the object and its
prototype object, including user-defined prototype methods. Are you sure
you do not want

Array.prototype.in_array = function(value)
{
for (var i = this.length; i--;)
{
if (this[i] == value)
{
return true;
}
}

return false;
}

instead? That said, in JavaScript 1.6 there is Array.prototype.indexOf()
already: <URL:http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6>
[color=blue]
> and put them in a separate file and included like,
>
> <script src="commonScripts.js" type="text/javascript"></script>
>
> and there's this script in the file it's included:
>
> str = 'this i re[4], Re[5], fw[100] ve Fw[200]';
> re = /(re|fw)\[\d+\]/ig;
> nevar = re.exec(str);[/color]

There is no practical point in calling RegExp.prototype.exec() this way.
Are you aware of String.prototype.match()?

And none of your identifiers was declared (with `var') which is considered
a Bad Thing.
[color=blue]
> ver = '\n\n';
> for (var ww in nevar)
> ver += ww + ': ' + nevar[ww] + '\n';[/color]

Highly inefficient. Consider

var ver = ["\n"];
for (var ww in nevar)
{
ver.push(ww + ': ' + nevar[ww]);
}
ver = ver.join("\n");

instead.
[color=blue]
> alert(ver);
>
> this script alerts all (and only) the prototype functions in the js file[/color]

No, it does not. See below.
[color=blue]
> (there are other fynctions and var definitions in it. this is the output:
> [...]
> input: this i re[4], Re[5], fw[100] ve Fw[200]
> index: 7
> lastIndex: 12
> 0: re[4]
> 1: re[/color]

Those are not prototype functions/methods.
[color=blue]
> how can i solve this problem?[/color]

Depends. What is your problem anyway? Everything works as designed.
What do you think does not work here, and what do you expect instead?


PointedEars
pinti
Guest
 
Posts: n/a
#7: Mar 13 '06

re: alerting all the protype functions


"Thomas 'PointedEars' Lahn" <PointedEars@web.de>, haber iletisinde sunlari
yazdi:2115496.dX2tCzlGOb@PointedEars.de...[color=blue]
> Depends. What is your problem anyway? Everything works as designed.
> What do you think does not work here, and what do you expect instead?
>[/color]

if you don't see any problem, well, then there isn't any.

as for me, the problem isn't using 'exec' instead of 'match' or 'test' or whatever, nor the
eficiency of some code just put as an example (and typed loosely), nor whether not using 'var' is
considered as bad thing. just run the following script actually or mentally.

<script type="text/javascript">
Array.prototype.in_array = function (value) {
var i, len = this.length;
for (i = 0; i < len; i++)
if (this[i] == value) return true;
return false;
}

dummy = [1, 'foo', 2, 'bar'];
s = '';
for (var i in dummy)
s += i + ': ' + dummy[i] + '\n';
alert(s);
</script>

output:

in_array: function (value) {
var i, len = this.length;
for (i = 0; i < len; i++)
if (this[i] == value) return true;
return false;
}
0: 1
1: foo
2: 2
3: bar

as far as i understand from other people's remarks in this thread, the solution is this: if you
prototype a method for Array, don't use for ... in type of iteration for arrays.

and what i learned as a bonus is: "there is no way to make a user-defined dynamic or class property
non-enumerable." as stated in
http://www.mozilla.org/js/language/j...ity-attributes



pinti
Guest
 
Posts: n/a
#8: Mar 13 '06

re: alerting all the protype functions


thank you all.


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#9: Mar 13 '06

re: alerting all the protype functions


pinti wrote:
[color=blue]
> "Thomas 'PointedEars' Lahn" [...] [wrote:][color=green]
>> Depends. What is your problem anyway? Everything works as designed.
>> What do you think does not work here, and what do you expect instead?[/color]
>
> if you don't see any problem, well, then there isn't any.[/color]

No, there is not, unless you state what the problem is.
[color=blue]
> [...] just run the following script actually or mentally.[/color]

Actually and mentally, the script does what it should do, unless you state
what it is that it was intended to do, and that intention contradicts with
what you perceived.

Make that a rule of thumb: There is no point in posting code and saying that
it does not work as supposed, when you do not state what it was supposed to
do.
[color=blue]
> [the same code again][/color]

Do. Not. Ever. Do. This. Again.
[color=blue]
> as far as i understand from other people's remarks in this thread, the
> solution is this: if you prototype a method for Array, don't use
> for ... in type of iteration for arrays.[/color]

That is also what I have said, which you have conveniently ignored while
rambling about the things you do not care about in programming, clearly
showing your incompetent, irresponsible attitude that was the actual cause
of your problem.

You have confused the Array object and the array data structure it
encapsulates. A stupid beginner's error that could have been avoided
by consulting any reference material first. As simple as that.
[color=blue]
> and what i learned as a bonus is: "there is no way to make a user-defined
> dynamic or class property non-enumerable."[/color]

There are no class properties in the languages we are talking about.
[color=blue]
> as stated in
>[/color]
http://www.mozilla.org/js/language/j...ity-attributes

This is a _Working Draft_ of a potential future specification, one that
is not at all implemented client-side. You have not even understood that,
and you are trying to tell me that I am incapable to understand what
you perceive as an error, but is in fact no error at all? You must be
kidding.


Score adjusted

PointedEars
Closed Thread