Skip to content

Instantly share code, notes, and snippets.

@jaffreyjoy
Last active September 8, 2022 15:33
Show Gist options
  • Save jaffreyjoy/01d0e46bb994fe4770a7c715c6b873aa to your computer and use it in GitHub Desktop.
Save jaffreyjoy/01d0e46bb994fe4770a7c715c6b873aa to your computer and use it in GitHub Desktop.
SSD hw

Public

  • A variable is given public scope if it is supposed to be accessed outside the class.
  • Eg:- In a Stack class, we would want the method push() to be public.
    class Stack{
        push(){

        }
    }

Private

  • A variable is given private scope if dont want it to be accessed directly/modified by any entity outside its own class.
  • In an explicit JS class such variables are prefixed with # to signify that they are private members of the class.
  • Eg:- In a Stack class, we would want the variable top to be private since the only operations that should be publically available to a Stack object are pushing, popping, getting the size, and accessing the value of the top but not modifying it(which are done using privileged methods (discussed below)) of the stack.
    class Stack{
        #top;
        push(){

        }
    }

Privileged

  • Privileged methods can be used to access private variables by exposing themselves as public methods
  • Eg:- In a Stack class, we would want the variable top to be private, but to access its value without allowing modification we would need its value to be exposed by a public method which gets the property of privileged because of its behaviour.
    class Stack{
        #top;

        push(){

        }

        get_top(){
            return this.#top;
        }
    }

Static

  • Static methods are used to provide exclusive functionality to a Class itself rather than to objects/instances of it
  • Eg:- In a Date class, we could have a static method called now which gives us the current time, which in context of a Date object would not make sense...since what would now() on a date object mean?...Eg: calling now() a date "29/03/1998" seems foolish. A Date object could be assigned the value of Date.now() but calling now() on a Date object wouldnt make sense as mentioned earlier and hence it is undefined for an object/instance of the Date class.
    class Date{
        static now(){

        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment