
기본형(Primitive Type) : Number, String, Boolean, null, undifined = 값을 그대로 할당
| 변수명 | 주소 | 데이터 | 
|---|---|---|
| a | @123 | 123 | 
참조형(Reference Type) : Object(array, Function, RegExp) = 값이 저장된 주소값을 할당(참조)
| 변수명 | 주소 | 데이터 | 
|---|---|---|
| obj | @123 | @1011 | 
        var obj = { //@1011 -- a : @1012 b:@1013
            a : 1, // @1012
            b : 'b' //@1013
        }
function 함수선언문(){
    return 'aa'
}
var b = function 기명함수표현식(){
    return 'bb'
}
var 익명함수표현식 = function(){
    return 'cc'
}
var bc = function(){
    console.log('1초마다 실행')    
}
setInterval(bc,1000)
var a = 10;
var obj = {
    a : 20,
    b : function(){
        var self = this;
        console.log(this) //20
        function c(){
            console.log(this) //10
            console.log(self.a) // 20
        }
    }
}
obj.b();
var callback = function(){
    console.dir(this);
}
var obj = {
    a : 1,
    b : function(cb){
        cb(this);
    }
}
obj.b(callback)
ex]
function a(){
    var x = 1; 
    function b(){
        console.log(X)
    }
    b();
}
a(); // x
console.log(x); // undefined : 외부에서는 x를 사용할 수 없다
ex] 클로저
function a(){
    var x = 1;
    return function b(){
        console.log(X)
    }
}
var c = a();
c(); // x -- 외부에서 사용할 수 있음. 수정은 불가
function a(){
    var _x = 1;
    return {
        get x(){return _x;}
        set x(v){_x = v;}
    }
}
var c=a();
c.x = 10; // 외부에서 수정도 가능
예) 차량별로 연료량 및 연비는 랜덤, 유저별로 차량 하나씩 고르면 게임시작
각 유저는 자신의 턴에 주사위를 굴려 랜덤하게 나온 숫자만큼 디오함
만약 연료가 부족하면 이동불가
가장 멀리간 사람이 승리
var car = {
    fuel : 10,
    power : 2,
    total : 0,
    run : function(km){
        var wasteFuel = km/this.power;
        if(this.fuel < wasteFuel){
            console.log('이동불가')
            return;
        }
        this.fuel -= wasteFuel;
        this.total += km;
    }
};
car.power = 10;
car.fuel = 1000;
var createCar = function(f,p){
    var fuel = f;
    var poser = p;
    var total = 0;
    return{ // 외부에 노출할 것만
        run : function(km){
            var wasteFuel = km/power;
            if(fuel < wasteFuel){
                console.log('이동불가')
                return;
            }
        }
    }
}
var car = craeteCar(10,2);
car.run(10) // 이렇게만 사용가능
자바스크립트는 프로토타입 기반 언어다. 클래스 기반 언어에서는 '상속'을 사용하는 반면, 프로토타입 기반언어에서는 '복제'의 개념으로 상속을 구현한다.
function Person(n,a){
    this.name = n;
    this.age = a;
    var gomu = new Person('고무곰', 30);
    var gomuClone1 = new gomu.__proto__.constructor('고무곰_클론1',10);
    var gomuClone2 = new gomu.constructor('고무곰_클론2',24);
    var gomuProto = Object.getPrototypeOf(gomu);
    var gomuClone3 = new gomuProto.constructor('고무곰_클론3',20);
    var gomuClone3 = new Person.prototype.constructor('고무곰_클론4',15)
}
function Person(n,a){
    this.name = n;
    this.age = a;
}
let gomu = new Person('고무곰',30);
var iu = new Person('아이유', 25);
// 메서드 각각 만듦
gomu.setOlder = function(){
    this.age += 1;
}
iu.setAge = function(){
    this.age += 1;
}
// 한개만 만드는 방법
Person.prototype.getOlder = function(){
    return this.age;
}
Person.__prototype__.age = 10;
iu.getOlder();
gomu.getOloer(); // 30
gomu.__prototype__.getOlder(); //10
function Person(name, age){
    this.name = name||'이름없음';
    this.age = age||'나이모름' ;    
}
Person.prototype.getName = function(){
    return this.name;
}
Person.prototype.getAge = function(){
    return this.age;
}
//----- 중복메서드
function Employee(name, age, position){
    this.name = name||'이름없음'; //중복
    this.age = age||'나이모름' ;  // 중복
    this.positoin = position || '직책모름';
}
//------ 상속
Employee.prototype = new Person();
Employee.prototype.constructor = Employee;   // Person에서 getName(), getAge() 상속
Employee.prototype.getPosition = function(){ // getPosition만 만들어면주면됨
    return this.position;
}
reference