자바스크립트 기본 문법2

반응형
반응형

 

 

 

- 클래스

var Human = function(type) {
    this.type = type || 'Human';
};

Human.isHuman = function(human) {
	return human instanceof Human;
};
Human.prototype.breathe = function() { 
   alert('h-a-a-a-m');
};

var Klom = function(type,firstName,lastName) {
   Human.apply(this,arguments);
   this.firstName = firstName;
   this.lastName = lastName;
 };
 
 Klom.prototype = Object.create(Human.prototype);
 Klom.prototype.constuctor = Klom; // 상속하는 부분
 
 Klom.prototype.sayName = function() {
    alert(this.firstName + '' +this.lastName);
  };
  
  var oldKlom = new Klom('humman','Klom','park');
  Human.isHuman(oldKlom); 
    
 
   
  

=>   클래스 기반 코드로

class Human { 
    constructor(type='Human') {
    this.type = type; 
    }
    
    static isHuman(human) {
    return human instanceof Human;
    }
    
    breathe() {
    alert('h-a-a-a-m');
    }
 }
 
 class Klom extends Human {
    constructor(type, firstName, lastName) {
     super(type);
     this.firstName = firstName;
     this.lastName = lastName;
     }
     
     sayName() {
     super.breathe();
     alert(`${this.firstName} ${this.lastName}`);
     }
 }
  
  const newKlom = new Klom('human','klom','park');
  Human.isHuman(newKlom);

프로미스 : 비동기때 주로 사용

const condition = true;

const promise = new Promise((resolve,reject) => {
  if (condition) {
    resolve('성공');
   } else {
    reject('실패');
    }
  });

promise.then((message)=>{
  console.log(message);
}).catch((error) => {
   console.error(error);
}).finally(() => 
   console.log('무조건'));
   
promise.then((message) => {
   return new Promise((resolve,reject) => {
      resolve(message);
   });
}).then((message2) => {
   console.log(message2);
   return new Promise((resolve,reject) => {
     resolve(message2);
   });
}).then((message3) => {
  console.log(message3);
  }).catch((error) => {
    console.error(error);
});    

 

function findAndSaveUser(Users) {
  Users.findOne({}, (err, user) => { // 첫 번째 콜백
    if (err) {
      return console.error(err);
    }
    user.name = 'klom';
    user.save((err) => { // 두 번째 콜백
      if (err) {
        return console.error(err);
      }
      Users.findOne({ gender: 'm' }, (err, user) => { // 세 번째 콜백
        // 생략
      });
    });
  });
}

=>

function findAndSaveUser(Users) {
  Users.findOne({})
    .then((user) => {
      user.name = 'klom';
      return user.save();
    })
    .then((user) => {
      return Users.findOne({ gender: 'm' });
    })
    .then((user) => {
      // 생략
    })
    .catch(err => {
      console.error(err);
    });
}
const promise1 = Promise.resolve('성공1');
const promise2 = Promise.resolve('성공2');
Promise.all([promise1, promise2])
  .then((result) => {
    console.log(result); // ['성공1', '성공2'];
  })
  .catch((error) => {
    console.error(error);
  });

 

 

async/await

function findAndSaveUser(Users) {
  Users.findOne({})
    .then((user) => {
      user.name = 'klom';
      return user.save();
    })
    .then((user) => {
      return Users.findOne({ gender: 'm' });
    })
    .then((user) => {
      // 생략
    })
    .catch(err => {
      console.error(err);
    });
}

 

반응형

'프로그래밍 언어 > 자바스크립트' 카테고리의 다른 글

자바스크립트 기본 문법1  (0) 2020.09.09

댓글

Designed by JB FACTORY