Skip to content

Instantly share code, notes, and snippets.

@JasonDeving
Last active March 26, 2016 04:01
Show Gist options
  • Save JasonDeving/45d2fd789fe638b22852 to your computer and use it in GitHub Desktop.
Save JasonDeving/45d2fd789fe638b22852 to your computer and use it in GitHub Desktop.
Object Oriented
# Object oriented programming
```js
var answer = {
get: function fn1() {
return this.val;
},
val: 42
};
```
#Polymorphism changing the function
To create an object there are two steps
```js
var firmAnswer = Object.create(answer);
firmAnswer.get = function fn2() {
return answer.get.call(this) + "!!";
}
```
step 1 instatiate the object
step 2 insert the data
```js
firmAnswer.get(3.14);
var a = firmAnswer.get();
console.log(a)
```
#Prototypes
##Classical model
```js
function Answer(value) {
this._val = value;
}
Answer.prototype.get = function fn1() {
return this._val;
};
var lifeAnswer = new Answer(42);
lifeAnwer.get();
function FirmAnswer(value) {
Answer.call(this, value);
}
FirmAnswer.prototype = Object.create(Answer.prototype);
FirmAnswer.prototype.constructor = FirmAnswer;
FirmAnswer.prototype.get = function fn2() {
return Answer.prototype.get.call(this) + "!!";
}
var luckyAnswer = new FirmAnswer(7);
luckyAnswer.get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment