...

View Full Version : hide prototype from enumeration



subhailc
01-26-2006, 06:00 AM
are there built-in methods to keep a prototyped method of a public class private?

e.g.,
Array.prototype.cycle=function(){return this[this.push(this.shift())-1];}
var arr=[1,2,3];
var str="";
for(var i in arr)str+=arr[i];
alert(str);//gives back the elements and the function definitiontyia

liorean
01-26-2006, 06:12 AM
No, but you can use Object.prototype.hasOwnProperty to tell if a property is inherited or not in the actual loop body.

But then again, why are you using the for..in loop on an array instead of a plain for loop with a numeric iterator variable?

subhailc
01-26-2006, 07:12 AM
why are you using the for..in loop on an array instead of a plain for loop with a numeric iterator variable?
it was just an example - having said that, it's not that out of the realm of feasibility - i'll often construct arrays as hash tables to use both associative and integer access - the added weight of array methods is neglible.

as regards the original question -

flash's proprietary actionscript has a method (ASSetPropFlags) that allows you to prototype core classes at will and hide whatever bits and pieces you wish from iteration. i had hoped for something similiar or a setter to the getter of isPropertyEnumerable.

liorean
01-26-2006, 08:13 AM
The JavaScript implemented in browsers is mostly ECMAScript 3 compatible only. It doesn't provide any way of making members non-enumerable. (Or creating getters and setters, or default functions if called etc.) You'll have to work around it.

subhailc
01-26-2006, 08:17 AM
thanks for your help as always liorean

voxecho
02-03-2006, 04:10 PM
I just wanted to say thank you for this thread :D i don't like the answer, but it was just the thread i was looking for.

Echo



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum