Skip to content

Instantly share code, notes, and snippets.

@BojoDimov
Created April 4, 2019 16:35
Show Gist options
  • Save BojoDimov/6f4b3877cad14404b557c04f8af7bd3e to your computer and use it in GitHub Desktop.
Save BojoDimov/6f4b3877cad14404b557c04f8af7bd3e to your computer and use it in GitHub Desktop.
<div style="display:flex;flex-direction:column;">
<button [click-once]="testClick">Function as parameter</button>
<!-- [undefined, undefined, undefined] -->
<button [click-once]="testClick2('baba', 'dqdo')">Arrow function closure</button>
<!-- ["baba", "dqdo", "4i4o"] -->
<button [click-once]="testClick3('baba', 'dqdo')">Decorator</button>
<!-- ["baba", "dqdo", undefined] -->
<button [click-once]="testClick.bind(this, 'baba', 'dqdo')">Inline bind</button>
<!-- ["baba", "dqdo", "4i4o"] -->
</div>
@Component({
selector: 'component',
templateUrl: './component.html'
})
export class Component {
prop1 = '4i4o';
testClick(param1, param2) {
console.log(this);
return [param1, param2, this.prop1];
}
testClick2 = (param1, param2) => () => this.testClick(param1, param2);
@Bind()
testClick3(param1, param2) {
console.log(this);
return [param1, param2, this.prop1];
}
}
//Decorator factory, returns decorator function
function Bind() {
return function (target, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
//target is this.prototype
let originalMethod = descriptor.value;
descriptor.value = function (...args) {
return () => originalMethod.call(target, ...args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment