0

Do you know how to create a name for a constructor object in javascript? I have a fiddle please look at this. http://jsfiddle.net/m8jLoon9/2/

ex.

// you can name the object by using this
function MyConstructorName() {}

// with this one, the name of the objct is the variable
var varConstructorName = function() {};


// MyConstructorName{}
console.log( new MyConstructorName() );

// varConstructorName{}
console.log( new varConstructorName() );

// I have a function that creates an object
// with the name arguments provided
function createANameSpace(nameProvided) {
    // how to create a constructor with the specified name?
    // I want to return an object



    // EDITED, this is wrong, I just want to show what I want on this function
    var TheName = function nameProvided() {};

    // returns an new object, consoling this new object should print out in the console
    // the argument provided
    return new TheName();
}

// create an aobject with the name provided
var ActorObject = createANameSpace('Actor');

// I want the console to print out
// Actor{}
console.log( ActorObject  );
2
  • 1
    the "name" in the console is provided interally to devtools, it's not something code itself can reflect. function.name must come from a function statement. you can use eval() to build a named function just like a hard-coded one, but since the name is not useful anyway, it's not worth it. Commented Oct 24, 2014 at 22:47
  • yes, it's on devtools, but at least, the name will be shown. :) Commented Oct 24, 2014 at 23:29

2 Answers 2

2

Its actually achieved quite simply as follows

Create it by:

var my_name_space = { first: function(){ alert("im first"); }, second: function(){ alert("im second"); } };

Access it by:

my_name_space.first();

or

my_name_space.second();

It is very similar to storing variables in an object:

var car = {type:"Fiat", model:500, color:"white"};

Except the "Fiat" is another function itself. You could consider the namespace being and object with functions.

Sign up to request clarification or add additional context in comments.

19 Comments

I'm not talking about accessing it, I just want the name of the constructor to be the name provided. My purpose is for to know the other developer what context they are in when using 'this' keyword in a console, and also I think it's better to named it. I don't want to named all my constructor using the first method. To hassle to be doing the same small thing. :) Anyway, the way you do it, it is only referencing a function, not actually naming it.
"this" refers to the nearest scope, hence the name of the function is your namespace. I believe naming your "outer object" -> the namespace would do the trick if you wrap all your functions inside this? - I am sorry if I am misunderstanding you.
console.log(this) with a literal object will print object{}, no name at all, but with constructor you will know or others what is 'this'
Have you seen the fiddle? try to look at the console, you will know what I mean.
I get it. I would suggest using this.name instead of this (if the functions are not anonymous). jsfiddle.net/m8jLoon9/5 That way you dont have to define the constructor each time.
|
1

This seems like an abuse of the language, but you can return arbitrarily named objects by doing something like this:

function createANamespace(nameProvided) {
  return {
    constructor: {name: nameProvided}
  };
}

I only tried this on chrome, so ymmv.

Edit: Or, if you really want to abuse the language:

function createANamespace(name) {
  return new Function('return new (function '+ name + '(){} )')
}

2 Comments

function createANamespace(name) { return new Function('return new (function '+ name + '(){} )') } This is closer for what I've been asking, but it returns a string?, also by doing this, looks like eval will fire? I am afraid of eval :( opps, sorry, it returns of type function.
wow! It works, after running the function returned, the name provided is actually the name of constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.