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  );
olanchuy
  • 405
  • 1
  • 4
  • 13
  • 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. – dandavis Oct 24 '14 at 22:47
  • yes, it's on devtools, but at least, the name will be shown. :) – olanchuy Oct 24 '14 at 23:29

2 Answers2

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.

L.H
  • 325
  • 1
  • 7
  • 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. – olanchuy Oct 24 '14 at 22:10
  • "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. – L.H Oct 24 '14 at 22:13
  • 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' – olanchuy Oct 24 '14 at 22:16
  • Have you seen the fiddle? try to look at the console, you will know what I mean. – olanchuy Oct 24 '14 at 22:19
  • I get it. I would suggest using this.name instead of this (if the functions are not anonymous). http://jsfiddle.net/m8jLoon9/5/ That way you dont have to define the constructor each time. – L.H Oct 24 '14 at 22:24
  • you only consoled the name, not the object itself – olanchuy Oct 24 '14 at 22:33
  • I think there are no solutions for what I've ask. It's not scope on javascript anymore. I guess I have to defined all of my constructor like 'function Name1() {}, function Name2() {}, ect...' to achieve what I want. – olanchuy Oct 24 '14 at 22:36
  • I am sorry I cant help you more. I am looking everywhere at this very moment to find a better, and stable, answer for you, since I thought I had it earlier.. – L.H Oct 24 '14 at 22:37
  • Oh one last: Isnt this what you want? Very close to your own first versions. http://jsfiddle.net/m8jLoon9/7/ – L.H Oct 24 '14 at 22:50
  • It's still the same. You just reference the constructor with hardcoded the name :) All I want is a function to create a name for the constructor, since it's invalid to use something like this ex. function [nameArgument]() {} where the nameArgument is the aegument of the function I want, just like in the example above. – olanchuy Oct 24 '14 at 22:53
  • Okay, the closts I can get to this in JS is: http://jsfiddle.net/m8jLoon9/8/ I'm game over if this is not helping you :( – L.H Oct 24 '14 at 22:58
  • http://jsfiddle.net/m8jLoon9/8/ nope, it's still not that. For doing also that you change the constructor to a string? lol it seems you don't get what I mean :D I'm not after the name of the the property of the object. I'm after for the name of the constructor object when the object is being printed to the console. I need is a function to create a namespace and return the object so that when that object is being consoled, it prints something like TheNamespaceIPassedInAnArgument{} As @tengbretson says, I think it's an abuse of the languge. – olanchuy Oct 24 '14 at 23:12
  • Okay - I haven't experienced that in my own JS-story, sorry. I hope you get it to work, wether it's an abuse of the language or not :) – L.H Oct 24 '14 at 23:19
  • It's okay, I finally get what I want, but kinda afraid, hmmm, but it works, thank you for the help anway :) cheers!. Here's the fiddle http://jsfiddle.net/m8jLoon9/9/ – olanchuy Oct 24 '14 at 23:24
  • Ahh, very cool. Looks like a hackers work to me :) Try validating with http://www.jslint.com/ to understand more about it, if you like. I'm gonna hit the bed - take care! – L.H Oct 24 '14 at 23:28
  • oh, eval really fires lol :D anway, so stupid for doing all this. I have a quick noob question there's a topic about literal or cosntructor but doesn't involve about speed/performance. http://stackoverflow.com/questions/4859800/should-i-be-using-object-literals-or-constructor-functions , is literal is much faster and light than constructor object? – olanchuy Oct 24 '14 at 23:35
  • It's a good question. The literal is lighter and faster. See this cool benchmark for details: http://jsperf.com/object-notation-vs-constructor I still think you shouldnt choose them on behalf of their speed but rather the needs you have during the implementation tho, as the answer in your linked post also leads to imo.. - One could always counter some of the perfomance by caching the constructors as well, at least some of the way. – L.H Oct 24 '14 at 23:47
  • yes, I think so too. Okay, thanks for the perf. I upvoted your answer but accepted one above since it answers my questions, but I won't use it, it's looks a hack lol better stick with coding it literally. thanks again. – olanchuy Oct 25 '14 at 00:13
  • Okay buddy - I fully understand. Thanks for sharing your thoughts as well. – L.H Oct 25 '14 at 11:37
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 + '(){} )')
}
tengbretson
  • 169
  • 7
  • `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. – olanchuy Oct 24 '14 at 23:19
  • wow! It works, after running the function returned, the name provided is actually the name of constructor. – olanchuy Oct 24 '14 at 23:22