ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Method Calls
    Study/JavaScript 2020. 5. 8. 14:28

    Method Calls

    Finally, let’s use our new methods to access and manipulate data from Dog instances.

     

    class Dog { 
    	constructor(name) { 
        	this._name = name; 
            this._behavior = 0; 
        } 
        
        get name() { 
        	return this._name; 
        } 
        
        get behavior() { 
        	return this._behavior; 
        } 
        
        incrementBehavior() { 
        	this._behavior++; 
        } 
    } 
    
    const halley = new Dog('Halley');

     

    In the example above, we create the Dog class, then create an instance, and save it to a variable named halley.

    The syntax for calling methods and getters on an instance is the same as calling them on an object — append the instance with a period, then the property or method name. For methods, you must also include opening and closing parentheses.

    Let’s take a moment to create two Dog instances and call our .incrementBehavior() method on one of them.

     

    let nikko = new Dog('Nikko'); // Create dog named Nikko
    nikko.incrementBehavior(); // Add 1 to nikko instance's behavior 
    let bradford = new Dog('Bradford'); // Create dog name Bradford 
    console.log(nikko.behavior); // Logs 1 to the console 
    console.log(bradford.behavior); // Logs 0 to the console

     

    In the example above, we create two new Dog instances, nikko and bradford. Because we increment the behavior of our nikko instance, but not bradford, accessing nikko.behavior returns 1 and accessing bradford.behavior returns 0.

     


    class Surgeon {
      constructor(name, department) {
        this._name = name;
        this._department = department;
        this._remainingVacationDays = 20;
      }
      
      get name() {
        return this._name;
      }
      
      get department() {
        return this._department;
      }
      
      get remainingVacationDays() {
        return this._remainingVacationDays;
      }
      
      takeVacationDays(daysOff) {
        this._remainingVacationDays -= daysOff;
      }
    }
    
    const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');
    const surgeonDurant = new Surgeon('Durant', 'Orthopedics');
    
    console.log(surgeonCurry.name);
    surgeonCurry.takeVacationDays(3);
    console.log(surgeonCurry.remainingVacationDays);

    'Study > JavaScript' 카테고리의 다른 글

    Inheritance II  (0) 2020.05.08
    Inheritance I  (0) 2020.05.08
    Object.keys()  (0) 2020.05.08
    Setters  (0) 2020.05.07
    getter와 setter 차이점  (0) 2020.05.07

    댓글

Designed by Tistory.