Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

    hide prototype from enumeration

    are there built-in methods to keep a prototyped method of a public class private?

    e.g.,
    Code:
    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 definition
    tyia

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    Last edited by liorean; 01-26-2006 at 09:07 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by liorean
    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.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    thanks for your help as always liorean

  • #6
    New Coder
    Join Date
    Jan 2006
    Location
    South Central, KS
    Posts
    81
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I just wanted to say thank you for this thread i don't like the answer, but it was just the thread i was looking for.

    Echo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •