From 92f82ba270dabd588ba76f06b5832d1b6650fc41 Mon Sep 17 00:00:00 2001 From: Seungkeun Lee Date: Mon, 7 Dec 2015 14:24:33 +0900 Subject: [PATCH] Add Example for Object-oriented-programming - How to make class - How to make sub class - How to make private property Change-Id: Ic1a67cf893d733ce950e50b20a34e39d125494f3 --- .../how-to-oop/how-to-implement-inheritance.js | 46 ++++++++++++++++++++++ examples/how-to/how-to-oop/how-to-make-class.js | 42 ++++++++++++++++++++ .../how-to-oop/how-to-make-private-property.js | 28 +++++++++++++ 3 files changed, 116 insertions(+) create mode 100755 examples/how-to/how-to-oop/how-to-implement-inheritance.js create mode 100755 examples/how-to/how-to-oop/how-to-make-class.js create mode 100755 examples/how-to/how-to-oop/how-to-make-private-property.js diff --git a/examples/how-to/how-to-oop/how-to-implement-inheritance.js b/examples/how-to/how-to-oop/how-to-implement-inheritance.js new file mode 100755 index 0000000..c3d3a05 --- /dev/null +++ b/examples/how-to/how-to-oop/how-to-implement-inheritance.js @@ -0,0 +1,46 @@ +"use strict"; + +var Person = require('./how-to-make-class.js'); + +class Employee extends Person { + constructor(given, family, company) { + super(given, family); + this.company = company; + } + + // method override + getGreetings() { + var msg = super.getGreetings(); + msg += " in " + this.company; + return msg; + } + + getBonus() { + return false; + } +} + +class President extends Employee { + getBonus() { + return true; + } +} + +module.exports = { 'Employee':Employee, + 'President':President }; + +if (require.main === module) { + // Test code + var john = new Employee('John', 'Peter', 'Samsung'); + console.log(john.name); + console.log(john.getGreetings()); + console.log('My bonus is ' + john.getBonus()); + + var vp = new President('Mike', 'Park', 'Samsung'); + console.log(vp.name); + console.log(vp.getGreetings()); + console.log('My bonus is ' + vp.getBonus()); + + console.log("People count : " + Person.howManyPeople()); + +} diff --git a/examples/how-to/how-to-oop/how-to-make-class.js b/examples/how-to/how-to-oop/how-to-make-class.js new file mode 100755 index 0000000..216275d --- /dev/null +++ b/examples/how-to/how-to-oop/how-to-make-class.js @@ -0,0 +1,42 @@ +"use strict"; + +// ES6 class reference +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes + +var people_count = 0; + +class Person { + constructor(given, family) { + this.given = given; + this.family = family; + people_count++; + } + + // property getter + get name() { + return this.given + ', ' + this.family; + } + + getGreetings() { + return "Hello, My name is "+ this.name; + } + + static howManyPeople() { + return people_count; + } +}; + +module.exports = Person; + +if (require.main === module) { + // Test code + var john = new Person('John', 'Peter'); + console.log(john.name); + console.log(john.getGreetings()); + + var park = new Person('John', 'Park'); + console.log(park.name); + console.log(park.getGreetings()); + + console.log("People count : " + Person.howManyPeople()); +} diff --git a/examples/how-to/how-to-oop/how-to-make-private-property.js b/examples/how-to/how-to-oop/how-to-make-private-property.js new file mode 100755 index 0000000..7be441a --- /dev/null +++ b/examples/how-to/how-to-oop/how-to-make-private-property.js @@ -0,0 +1,28 @@ +"use strict"; + +var my_secret_key = Symbol(); +var my_secret_function = Symbol(); + +class Secret { + constructor() { + this[my_secret_key] = Math.random()*100; + this[my_secret_function] = function(args) { + console.log('orignal data is = '+ args); + } + } + + GetUserSecretKey(data) { + this[my_secret_function](data); + var crypto = require('crypto'); + var sha1 = crypto.createHash('sha1').update(data + this[my_secret_key]); + return sha1.digest("hex"); + } + +} + +module.exports = Secret; + +if (require.main === module) { + var obj = new Secret(); + console.log(obj.GetUserSecretKey('hello')); +} -- 2.7.4