How to define a function with dynamical name Back

Sometimes, we may meet the case when we need to initialize a function with dynamical name, especially when we want to implement Class extending in JavaScript. Therefore, we may need to know the working around of this problem according to

function nameFunction(name, functionBody) {
    return name ?
        new Function ('cb', 'return function '
            + name
            + '(cb) { return, arguments); }')
        : body;

Then, we can use this function to implement extending Class under ES5:

function nameFunction(name, functionBody) { /** ... */ }
function extendClass(parent, name, prototype, initialize, callSuper) {
    var constructor = nameFunction(name, function () {
        var self = (this instanceof constructor) ?
            : Object.create(constructor.prototype);
        self = (callSuper && parent.apply(self, arguments)) || self;
        return (initialize && initialize.apply(self, arguments)) || self;

    constructor.prototype = Object.assign(Object.create(parent.prototype), prototype);
    constructor.prototype.constructor = constructor;
    return constructor;
Empty Comments
Sign in GitHub

As the plugin is integrated with a code management system like GitLab or GitHub, you may have to auth with your account before leaving comments around this article.

Notice: This plugin has used Cookie to store your token with an expiration.