https://github.com/kornil/learning-oop-and-oloo-js
just my notes on javascript different coding styles
https://github.com/kornil/learning-oop-and-oloo-js
oop oop-principles
Last synced: 5 months ago
JSON representation
just my notes on javascript different coding styles
- Host: GitHub
- URL: https://github.com/kornil/learning-oop-and-oloo-js
- Owner: Kornil
- Created: 2017-01-08T12:32:03.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-08T17:54:58.000Z (over 9 years ago)
- Last Synced: 2025-06-13T09:44:10.788Z (about 1 year ago)
- Topics: oop, oop-principles
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# learning-oop-and-oloo-js
## add methods on the global Object so every new obj(regardless of how you create it) will have it by default
```javascript
// Place method inside the global Object so every new object will have that method by default
Object.prototype.greet = function(){
console.log("hello "+this.x)
}
var obj1 = {
x: "John"
}
var obj2 = new Object();
obj2.x = "Jane";
obj1.greet(); // hello John
obj2.greet(); // hello Jane
```
## create a "proto" object and use it to store all methods for other objects
```javascript
// create a "proto" object whith the sole purpose of providing methods to other different objectss
var proto = {
greet: () => console.log('hello world')
}
var obj1 = Object.create(proto);
obj1.greet(); // hello world
/////
///// "proto" with ES6 classes
/////
class Proto {
greet(){
console.log('hello world!')
}
}
class Obj2 extends Proto{
// your code here
}
var obj2 = new Obj2();
obj2.greet(); // hello world!
```