Skip to content

Instantly share code, notes, and snippets.

@behnammodi
Created October 22, 2020 13:45
Show Gist options
  • Save behnammodi/42b003a166d931c9b55eb1ff5084b6b9 to your computer and use it in GitHub Desktop.
Save behnammodi/42b003a166d931c9b55eb1ff5084b6b9 to your computer and use it in GitHub Desktop.
For review
// -----------------------------------------------------------------------------------------
/**
* 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