JSFiddle - React, Tailwind, and code Playground

by Lyndsy Simon

Babel + JSX

class Foo {
	constructor() {
  	this.my_attribute = 42;
  }
	my_method() {
  	return this.my_attribute;
  }
}

let decorate_without_binding = function (f) {
	return function () {
		return '<decorate_without_binding> The value is: ' + f(arguments);
  }
}

let decorate_with_binding = function (obj, f) {
	return function () {
  	return '<decorate_with_binding> The value is: ' + f.apply(obj, arguments);
  }
}


console.log('== bar ==')
let bar = new Foo();
console.log('Before decorating: ' + bar.my_method());
bar.my_method = decorate_with_binding(bar, bar.my_method);
console.log('After decorating: ' + bar.my_method());


console.log('== foo ==')
let foo = new Foo();
console.log('Before decorating: ' + foo.my_method());
foo.my_method = decorate_without_binding(foo.my_method);
// raises an error; `this` is undefined in the method
foo.my_method();