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.
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 51

Thread: OOP JavaScript

  1. #31
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is nu such thing as "unchangeable" in JScript,as it doesn't have features that allow you to protect your data from being changed.
    Not yet! I've read documentation that says CONST is a reserved word in JS - but not implemented.

  2. #32
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    CONST would be a very welcome addition.

    I don't know if the following is a "closure," but it keeps properties from changing, provided you don't mind the extra coding:
    Code:
    function Constructor () {
       self = this; // fixes privatization bug
       this.publicVar = 'some public value, hence changeable';
       var hiddenVar = 'some constant value';
       this.getHiddenVar = function () {
          return hiddenVar;
       }
    }
    instance = new Constructor();
    public = instance.publicVar; // works fine
    // hidden = instance.hiddenVar; // throws error
    hidden = instance.getHiddenVar(); // works fine
    instance.hiddenVar = 'different value'; // creates a new non-inheritable property of this object
    alert(instance.hiddenVar == instance.getHiddenVar()); // alerts 'false'
    Using an accessor method prevents access to the actual value stored in the initialized object.

  3. #33
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is quite strange,as I am SURE I've read an example of using const in "JavaScript Unleashed",where it said that u can use const,altough no checking is done to stop you from modifing the value...

  4. #34
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Code Wizard
    This is quite strange,as I am SURE I've read an example of using const in "JavaScript Unleashed",where it said that u can use const,altough no checking is done to stop you from modifing the value...
    Javascript Unleashed - 3rd edition says "Javascript does not support constants..." The CONST reserved word is not mentioned.

    Pure Javascript - 2nd edition says "the word const is reserved for future use. There is no definition at this time."

  5. #35
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah yes, closures - I use them quite a bit in JS
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  6. #36
    Regular Coder
    Join Date
    Mar 2003
    Posts
    241
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Very interesting thread!
    I have started OOP:ing just recently so this thread has been very useful so far


    One question though which I could not find an answer to: [I've searched the forums and googled for it]

    Where can I read more about this bug, I have no idea of why it's needed or what it does ?
    Quote Originally Posted by Choopernickel
    ...
    Code:
    function Constructor () {
       self = this; // fixes privatization bug
       ...

  7. #37
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    RadarBob

    I have the book,and I quite clearly says what I've posted above,also providing an example with the const keyword in use.
    I think this mistake appears because it's a version translated in romanian...


    Caffeine

    I have just the link 4 u:
    http://www.crockford.com/javascript/private.html


    It isn't a bug...it's just that JScript has a rather simplistic OO model,I don't really believe that it was supposed to support such a thing as private data...
    Closures are just a workaround for this,that's why you don't see them in any book related to JavaScript...or am I wrong...?
    Last edited by Code Wizard; 04-14-2004 at 11:19 AM.

  8. #38
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So... is what I posted indeed a closure?

  9. #39
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I agree with Code Wizard - this really doesn't seem like a bug - just a shortcoming of JS's OO. A "bug" implies that it's supposed to do something and doesn't - when I don't believe it was supposed to.

    A closure is basically when you have access to function-level scope after that function has already returned. A prime exaple is if you use a function to apply event handlers.

    Code:
    function myObject( id )
    {
        var self = this; // function-level scope reference to object
        this.obj = document.getElementById( id );
    
        this.clickHandler = function()
        {
            alert( "I've been clicked!" );
        }
    
        this.obj.onclick = function() { self.clickHandler() } // this is the closure
    }
    Unless I'm misunderstanding the concept, this is a good example. The clickHandler() method can be called at anytime - certainly long after the myObject constructor has returned - however, through the scope chain we've established, it still has access to the function-level scoped variable self
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  10. #40
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As a side note,can anyone give a good explanation of `self ' ,does it only reffer to the current object,like the this keyword...I thought it was used like the window object (instead of writing window.prop ,you could write self.prop - so does this apply to any object type aswell...?)

  11. #41
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, well, it's simpler than that.

    In the global context, self exists as a reference to the current window. But, it's pretty useless, IMO. Why? We already have window for that.

    So, not being worried about writing over self (or having trouble accessing the global one), it's the perfect variable name to use for a reference to the current object. However, one could just as easily choose another name for this variable.

    So, in the context of my example, it has no automatic value - it's just a variable, which I explicity declare
    Code:
    var self = this;
    Got that?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  12. #42
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Maybe this is a non-confusing name

    var me = this;


  13. #43
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah yes,indeed,now I see the light
    Hence I never used this techinique(if i could called it that),of overwriting default objects;
    self is there for flexibility,u can use it instead of window...
    But aren't some core objects write protected,can I just overwrite them all...?
    Can I just set window.opener(on a new window) to null ,or set the document object to null...is there any advantage in doing so ?
    Wow! Not that's a lot of questions !
    Hope there's any1 to answer them...
    Anywayz,I must agree this thread is progressing quite nicely

  14. #44
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would avoid the overwriting of core objects for a couple reasons:

    They may be unprotected in the browser you use but who can say about all browsers?

    Some other script may count on that value not being changed.

    However what Beetle did wasn't the same (assuming that he did that within a function which is a reasonable assumption), he defined a new locally scoped variable which only happens to have the same name as a global object which only makes that global object unavailable within that scope and wouldn't be a problem for this particular object.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  15. #45
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes,I know he was just declaring a local variable,by using var.
    If we have a variable defined inside a function with the same name as a global one,like this:
    Code:
    var value
    function F()
    {
    var value
    ...
    }
    Then we could acces the global variable from within the function like this:
    Code:
    window['value']=...
    or
    window.value=...
    or
    self['value']=...
    or
    self.value=...
    But the real interesting bit,is how could we access the self object(equivalent of the window object) inside the function were we already have defined a local variale called self...
    Now,if there would be no equivalent for self,we could not access this object from within the function.
    The same would be if we had a variable called window declared inside a function...

    In the global context, self exists as a reference to the current window. But, it's pretty useless, IMO. Why? We already have window for that.
    Now,do you see why we have both window and self?
    It's called flexibility.
    Now,I'm not saying this example is it's the only reason why we have window and self,but it's certainly a thing to keep in mind.
    Last edited by Code Wizard; 04-15-2004 at 11:17 PM.


 
Page 3 of 4 FirstFirst 1234 LastLast

Posting Permissions

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