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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    85
    Thanks
    13
    Thanked 1 Time in 1 Post

    quick name conflict question

    can I have a function and a variable be the same name in an object

    for instance

    Code:
    function someObject(){
       this.canDoSomething;//a boolean variable
    
       function canDoSomething(){
           //set this.canDoSomething;
           return this.canDoSomething;
       }
    }
    or will this be a naming conflict?
    thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Not to ask a dumb question, but...

    Why didn't you just try it?

    Code:
    <script>
    function someObject(){
       this.canDoSomething = true;//a boolean variable
    
       function canDoSomething(){
           //set this.canDoSomething;
           return this.canDoSomething;
       }
    }
    
    var so = new someObject();
    alert( "variable: " + so.canDoSomething );
    alert( "function: " + so.canDoSomething() );
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by footballer27 View Post
    can I have a function and a variable be the same name in an object
    The answer is no, but even if it were possible it would be very poor practice to have identical names.

    It is your responsibility to die() if necessary….. - PHP Manual

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    footballer27 (01-07-2012)

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    85
    Thanks
    13
    Thanked 1 Time in 1 Post
    So, It will be a conflict? right? Thanks


  •  

    Posting Permissions

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