0

I have this code:

function geno(name, obj, parent) {
    parent = parent || null;
    window[name] = function() {
        if (parent){
            parent.call(this);
        }
        for (name in obj) {
            this[name] = obj[name];
        }
    }
    if (parent) {
        window[name].prototype = Object.create(parent.prototype);
    }
}

I want to be able to take this data

{Person:{name:"Sherlock Holmes",address:"221b Baker St."}

And turn it into the class

function Person() {
    this.name = "Sherlock Holmes";
    this.address = "...";
}

and have it be a named function. Currently, it's an anonymous function. Is there a method of doing this that creates a named function instead?

Corbbin Goldsmith
  • 387
  • 1
  • 3
  • 9
  • oh... I left something out. – Corbbin Goldsmith Dec 14 '15 at 03:25
  • 1
    Depending on what you mean by "named function", there is no way to create it other than the explicit syntax "function name()". Why do you want the function to be named? –  Dec 14 '15 at 03:34
  • So I can create named functions from a JSON file. – Corbbin Goldsmith Dec 14 '15 at 03:50
  • First of all, it's not JSON, and it's not a JSON file. It's a JavaScript object. And what I was saying is no, you **cannot** create a named function programatically--if by "named function" you mean a function with the `name` property. That is set only when the interpreter processes a syntactic construct of the form `function foo()`. It cannot be set programatically. And what I was also trying to understand is why you want a named function. The only place a function's name in the sense of the `name` property is used to my knowledge is in the debugger. –  Dec 14 '15 at 03:53
  • Okay, so it's not actually necessary to make it a named function. – Corbbin Goldsmith Dec 14 '15 at 05:54
  • If I do find a way to make it named, regardless, I'll post my answer. – Corbbin Goldsmith Dec 14 '15 at 05:55
  • So your question is how to make it named, but in your comment you say you do not need it to be named--so is there any remaining question? –  Dec 14 '15 at 06:39
  • If there's a way, I want it, but until then, it's not 100% necessary. It's mostly for debugging purposes. Would it be possible to do it with Object.defineProperty()? – Corbbin Goldsmith Dec 18 '15 at 03:18
  • @torazaburo: Look at [Is there any non-eval way to create a function with a runtime-determined name?](http://stackoverflow.com/q/9479046/1048572) – Bergi Dec 18 '15 at 04:20
  • Or wait, this a duplicate, right? – Bergi Dec 18 '15 at 04:21

1 Answers1

0

The fn.name property is not writable by default, but it is configurable, so

function geno(name, obj, parent) {
    parent = parent || null;
    var fn = window[name] = function() {
        if (parent) parent.call(this);
        for (name in obj) this[name] = obj[name];
    };

    /////////////////////////////////////////////
    // MAKE "NAME" PROPERTY WRITABLE, THEN SET IT
    Object.defineProperty(fn, "name", { writable: true });
    fn.name = name;
    /////////////////////////////////////////////

    if (parent) fn.prototype = Object.create(parent.prototype);
}

However, this might not work in older implementations. From MDN:

Notice that in non-standard, pre-ES6 implementations the configurable attribute was false as well.