Skip to content

Instantly share code, notes, and snippets.

@domenic
Last active January 25, 2016 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenic/c1566e755c9e14613aa1 to your computer and use it in GitHub Desktop.
Save domenic/c1566e755c9e14613aa1 to your computer and use it in GitHub Desktop.
ES2015 constructor call trick
let returnMe;
class HTMLElement {
constructor() {
if (returnMe) {
const temp = returnMe;
returnMe = null;
return temp;
}
// ...
return allocateFromUACode();
}
}
class XFoo extends HTMLElement {
constructor() {
super(); // effectively does this = returnMe
}
}
function upgradeEl(el) {
returnMe = el;
new XFoo();
}
@domenic
Copy link
Author

domenic commented Jan 25, 2016

Problem:

class XFoo extends HTMLElement {
    constructor() {
        new XFoo(); // clobbers returnMe
        super();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment