Created
October 22, 2020 13:45
-
-
Save behnammodi/42b003a166d931c9b55eb1ff5084b6b9 to your computer and use it in GitHub Desktop.
For review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------------------------------- | |
/** | |
* object literal | |
*/ | |
const circle = { | |
r: 1, | |
draw: () => console.log('i draw'), | |
}; | |
circle.draw(); | |
// ----------------------------------------------------------------------------------------- | |
/** | |
* factory function | |
*/ | |
function createCircle(r) { | |
return { | |
r, | |
draw: () => console.log('i draw'), | |
}; | |
} | |
const circle = createCircle(1); | |
circle.draw(); | |
// ----------------------------------------------------------------------------------------- | |
/** | |
* constructor function | |
*/ | |
function Circle(r){ | |
this.r = r; | |
this.draw = () => console.log('i draw') | |
return this; | |
} | |
const circle = new Circle(1); | |
// or | |
const circle = Circle.call({}, 1); | |
// or | |
const circle = Circle.bind({},1)(); | |
// or | |
const circle = Circle.apply({},[1]); | |
// with inheritance | |
const shape = { | |
name: 'shape', | |
getName: ()=> { | |
return 'shape' | |
} | |
}; | |
const circle = Circle.call(shape, 1); | |
circle.draw(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment