Create Constructor Dynamically?

One line of some Adobe Animate CC code I am experimenting with is like so:

var exportRoot = new libs.chartCanvasG();

I understand this to be a constructor. I have a function that initializes some information before. I would like to add a property that becomes the name of this constructor (in this case chartCanvasG) to my function. Sort of like…

function init(stage,canvas,constructorName) {
    var constructorName = ???

How could this be done? Being it is a constructor that is started with the new keyword does it have to be a prototype? I don’t even know where to start with the syntax. Thanks

Is there any particular reason why you need that?

You can get the constructor’s name with

I got what I was looking for via the eval() function. Thanks

It is possible without eval. Depending on your context, you would want to avoid eval completely.

let libs = {}
function init(constructorName) {
	libs[constructorName] = function () { return this}
	return libs
new libs.canvasInstrucion()

You have to define what that function is and what it does. But as long as it returns a class or a function returning this, you can have flexible constructors.

Thank so much for the reply. I new about the concerns and recommendations about eval() (line 50), I did not think I could use a more object based way because I was not ready to return something at that point. My functions don’t return anything here. I like the syntax you write, but can that fit in here?

$(document).ready(function () {
	function init(anId,canvasId,acId,doId,AnID,libName) {
		canvas = document.getElementById(canvasId);
		var anim_container = document.getElementById(acId);
		var dom_overlay_container = document.getElementById(doId);
		var comp=AnID.getComposition(anId);
		var lib=comp.getLibrary();
		var loader = new createjs.LoadQueue(false);
		loader.addEventListener("fileload", function(evt){handleFileLoad(evt,comp)});
		loader.addEventListener("complete", function(evt){handleComplete(evt,comp,AnID,libName)});
		var lib=comp.getLibrary();
	function handleFileLoad(evt,comp) {
		var images=comp.getImages();	
		if (evt && (evt.item.type == "image")) { images[] = evt.result; }	
	function handleComplete(evt,comp,AnID,libName) {
		//This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
		var lib=comp.getLibrary();
		var ss=comp.getSpriteSheet();
		var queue =;
		var ssMetadata = lib.ssMetadata;
		for(i=0; i<ssMetadata.length; i++) {
			ss[ssMetadata[i].name] = new createjs.SpriteSheet( {"images": [queue.getResult(ssMetadata[i].name)], "frames": ssMetadata[i].frames} )

		var exportRoot = eval("new lib." + libName + "()");
		var stage = new lib.Stage(canvas);	
		//Registers the "tick" event listener.
		var fnStartAnimation = function() {
			createjs.Ticker.addEventListener("tick", stage);
	function setIntervalnow(func, interval) {
		return setInterval(func, interval);
	var  myInterval = setIntervalnow( frstRun => {	


		window.setTimeout(function () {
		}, 1500 );
	}, 14000);


In your case, you can simply use variable object pointers.

var exportRoot = new lib[libName]()

you can do the same with arrays.

var pointer = 'c'
var list = ['a', 'b', 'c', 'd']
return list[pointer]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.