Javascript class example
본문 바로가기

Frontend/모던자바스크립트

Javascript class example

What's the purpose of using Javascript Classes?

Classes are used to create and manage multiple Objects

 

Old Javascript class

function 붕어빵(title, ingredients){
	this.title = title,
  	this.ingredients = ingredients
}
var 슈크림붕어빵 = new 붕어빵('슈크림붕어빵',['밀가루','물','슈크림','설탕'])

Modern Javascript class

class 모던붕어빵{
    constructor(title, ingredients){
	this.title = title,
  	this.ingredients = ingredients
    }
}
let 피자붕어빵 = new 모던붕어빵('피자붕어빵',['밀가루','설탕','물','치즈','케쳡','소세지','양파','피망'])


//Typescript
class 모던붕어빵2{
	title:string;
    indegredients:string;
    constructor(title2:string, ingredients2:string){
	this.title = title2,
  	this.ingredients = ingredients2
    }
}

 

prototype

the prototype property allows you to add properties and methods to any object.

function 게임(){
    this.q = 'move left';
    this.w = 'move right';
}
게임.prototype.name = '바람의나라'

var game = new 게임()
console.log(game.name)

if there is no name of game, javascript search '게임' prototype.

 

Array and Object have their own prototypes, so you can easily access them and coding!

반응형