# Parameter & Argument

Let’s compare the difference between parameters and arguments.

 123 function change (price, moneypaid) { return moneypaid - price }; 

price and moneypaid are parameters in the function above.

 12  function change (100, 150); 

100 and 150 are arguments in the function above.

The name changes depending on each case.

## Rest Parameter

If there are more than one arguments and the number can constantly change? Use Rest Parameter!

Refer to the function that works as Math.min as an example.

 1234567891011121314151617  function minimum (...nums) { return nums.reduce( function (prev, cur) { if (prev > cur){ return cur } else { return prev; } }); } 

The function above uses reduce to find the smallest number. Using a rest parameter as …nums allows the parameters to be received in the form of array. This is possible from ES6 and not in ES5 because although it may look like an array, it is actually not. Array methods cannot be used.

 12345678910  function what(){ console.log(arguments) } what (1, 3, 4, 5); //{0:1, 1:3, 2:4, 3:5} . 

Next is Default Parameter. Like the name, default parameter can be used when you want a default value.

 12345678910111213  function change (price, moneypaid = 200) { return moneypaid - price }; change(50); // 150 change(50, undefined); // 150. 

# OOP

After learning about parameters, it’s now time to learn about Object Oriented Programming.

• To put it simply, object oriented programming can be explained as creating a blueprint that acts as a model and create many objects based on the blueprint. Here, blueprint is class and object is instance.

For example,

If class is

function car (color) {},

instance is

let bmw = new car (“blue”);

let avante = new car (“white”);

and etc.

In ES5, class needed to be created as function car (brand, name, color) {}, but from ES6, it can be crated using a class keyword.

 1234567 class car { constructor (brand, name, color){ } // Code for creating Instance } 

Properties and methods defined when making a class can be used in instances. Examples of property can be brand, color and name while example of method can be refuel(), driveWithCruise() and etc.

Properties can be set using “this”.

 123456789101112131415161718192021222324252627282930  ES5) function car (brand, name, color) { this.brand = brand; this.name = name; this.color = color; } ES6) class car { constructor (brand, name, color){ this.brand = brand; this.name = name; this.color = color; } } 

Setting method works in a similar way.

 1234567891011121314151617181920212223242526272829303132333435363738394041  ES5) function car (brand, name, color) { car.prototype.refuel = function () {} car.prototype.drivewithcruise = function () {} } ES6) class car { constructor (brand, name, color){ } refuel () { } drivewithcruise () { } } Example) let bmw = new car (benz, li750, black); bmw.brand; // benz bmw.name; //li750 bmw.refuel() //연료 충전! bmw.drivewithcruise() //크루즈 모드~ 

Prototype like the word means the blueprint of all instances.

Instance is the model that you make based on prototype.

Constructor is the constructor function used to create class.

## this

this is a context that changes by situation, and in this case refers to the instance that is being created by the new keyword.

An easy example to understand is:

 1234567891011  let example = new Array(1, 3, 5); example.length; //3 example[0]; //1 example.pop(); //[1, 3]; 

All the array methods in MDN are methods of Array class.

Finito

0%