473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

alerting all the protype functions

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?


Mar 12 '06 #1
8 1096
pinti wrote:
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';
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);

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:

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
Mar 12 '06 #2
"Jonas Raoni" <jo********@gmail.com>, haber iletisinde sunlari yazdi:dv**********@emma.aioe.org...
pinti wrote:
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';


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);

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:

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]);


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?
Mar 12 '06 #3
On 12/03/2006 19:48, pinti wrote:
"Jonas Raoni" [wrote]:
[snip]
Do not prototype the Array nor the Object,
If adding a method or property to the prototype object of either is the
best solution, then do it.
by doing it you damage the "for..in" loop, [...]


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]
Array.prototype.in_array = function (value) {
for (var hoba in this)
if (this[hoba] == value) return true;
return false;
}
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]
(in this case, why doesn't if list all the predefined or built-in
Array methods?)


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.
Mar 12 '06 #4
Michael Winter wrote:
On 12/03/2006 19:48, pinti wrote:
"Jonas Raoni" [wrote]:
Do not prototype the Array nor the Object,
If adding a method or property to the prototype object of either is the
best solution, then do it.
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 =/
by doing it you damage the "for..in" loop, [...]
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.


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 =]

Array.prototype.in_array = function (value) {
for (var hoba in this)
if (this[hoba] == value) return true;
I think the "===" would be a better operator.
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.


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.
(in this case, why doesn't if list all the predefined or built-in
Array methods?)


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.


Yeah =]

alert([].propertyIsEnumerable("join"));
--
Now with alcohol <URL:http://youtube.com/watch?v=lnQTZxqxc10> =X
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 12 '06 #5
pinti wrote:
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;
}
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>
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);
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.
ver = '\n\n';
for (var ww in nevar)
ver += ww + ': ' + nevar[ww] + '\n';
Highly inefficient. Consider

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

instead.
alert(ver);

this script alerts all (and only) the prototype functions in the js file
No, it does not. See below.
(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
Those are not prototype functions/methods.
how can i solve this problem?


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
Mar 13 '06 #6
"Thomas 'PointedEars' Lahn" <Po*********@web.de>, haber iletisinde sunlari
yazdi:21****************@PointedEars.de...
Depends. What is your problem anyway? Everything works as designed.
What do you think does not work here, and what do you expect instead?


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

Mar 13 '06 #7
thank you all.
Mar 13 '06 #8
pinti wrote:
"Thomas 'PointedEars' Lahn" [...] [wrote:]
Depends. What is your problem anyway? Everything works as designed.
What do you think does not work here, and what do you expect instead?
if you don't see any problem, well, then there isn't any.


No, there is not, unless you state what the problem is.
[...] just run the following script actually or mentally.
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.
[the same code again]
Do. Not. Ever. Do. This. Again.
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.
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.
and what i learned as a bonus is: "there is no way to make a user-defined
dynamic or class property non-enumerable."
There are no class properties in the languages we are talking about.
as stated in

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
Mar 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
2
by: Koen | last post by:
Hi all, I have a question about something rather common, but I'm a bit lost at the moment: Lat's say you have this: // in A.h Class A {
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.