Advertisement
MUstar

Javascript ES6 Example - Classes

Jul 27th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //ES6 Classess
  2.  
  3. //Prototype
  4. var nemo = class Rectangle {
  5.     constructor(width,height){
  6.         this.width = width;
  7.         this.height = height;
  8.     }
  9.     getResult(){
  10.         return this.calculate();
  11.     }
  12.     calculate(){
  13.         return this.height * this.width;
  14.     }
  15. };
  16.  
  17. var calcnemo = new nemo(5,5);
  18. document.write('Result : ' + calcnemo.getResult() + '<br/>'); // 25
  19.  
  20. //static
  21. class Point {
  22.     constructor(x,y){
  23.         this.x = x;
  24.         this.y = y;
  25.     }
  26.     static distance(a,b){
  27.         const dx = a.x - b.x;
  28.         const dy = a.y - b.y;
  29.         return dx * dx + dy * dy;
  30.     }
  31. }
  32.  
  33. const p1 = new Point(5,8);
  34. const p2 = new Point(8,5);
  35. document.write('Point : ' + Point.distance(p1,p2) + '<br/>'); // 18
  36.  
  37. //extends _ sub classing
  38. class pre {
  39.     constructor(){
  40.         this.jsword = "자바스크립트";
  41.         this.webword = "웹";
  42.         this.five = 5;
  43.     }
  44.     word(){
  45.         return "Hello World!";
  46.     }
  47.     getjsword(){
  48.         return this.jsword;
  49.     }
  50.  
  51. };
  52.  
  53. var Test = class TestClass extends pre{
  54.     word(){
  55.         return "Hello World!";
  56.     }
  57.     plus_five(num){
  58.         return this.five + num;
  59.     }
  60. };
  61.  
  62. var varpre = new pre();
  63. document.write(varpre.jsword  + '<br/>'); //자바스크립트
  64. document.write(varpre.getjsword()  + '<br/>'); //자바스크립트
  65. document.write(varpre.word()  + '<br/>'); //Hello World!
  66.  
  67.  
  68. var varTest = new Test();
  69. document.write(varTest.word() + '<br/>'); //Hello World!
  70. // document.write(varTestClass.word() + '<br/>'); // is not defined
  71. document.write(varTest.plus_five(10)  + '<br/>'); // 15
  72.  
  73. function food (name){
  74.     this.name = name;
  75. }
  76.  
  77. food.prototype.flavor = function (){
  78.     document.write(this.name + '는 달콤하다.<br/>');
  79. };
  80.  
  81. class chocolate extends food {
  82. }
  83.  
  84. class ramen extends food{
  85.     flavor(){
  86.         document.write(this.name + '라면은 맛있다.<br/>');
  87.     }
  88. }
  89.  
  90. class milk extends food{
  91.     flavor(){
  92.         super.flavor();
  93.         document.write(this.name + '는 완벽한 에너지드링크!<br/>');
  94.     }
  95. }
  96.  
  97. var ramen_talk = new ramen('치킨');
  98. ramen_talk.flavor(); //치킨라면은 맛있다.
  99.  
  100. var choco_talk = new chocolate('밀크초코');
  101. choco_talk.flavor(); //밀크초코는 달콤하다.
  102.  
  103. var milk_talk = new milk('더 진한 커피우유');
  104. milk_talk.flavor();//더 진한 커피우유는 달콤하다.
  105.                    //더 진한 커피우유는 완벽한 에너지드링크!
  106.  
  107.  
  108. //Mix-ins
  109.  
  110. //=> arrow function
  111. var calculatorMixin = Base => class extends Base {
  112.     calc() {}
  113. };
  114.  
  115. var ramdomizerMixin = Base => class extends Base{
  116.     randomize(){}
  117. };
  118.  
  119. class Foo {};
  120. class Bar extends calculatorMixin(ramdomizerMixin(Foo)){};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement