www.webdeveloper.com
Results 1 to 4 of 4

Thread: OOP Javascript-Determine instance's object type and make a new instance of that type

  1. #1
    Join Date
    Dec 2003
    Location
    FL
    Posts
    93

    Question OOP Javascript-Determine instance's object type and make a new instance of that type

    I've been programming in Javascript for over 10 years now, but only in the last year or two have I started working with Javascript's OOP functionality (classes, instances, etc.). I know how to work with classes in C++ and PHP, but Javascript is a different story.

    So let's say I have an array of instances of different objects. For a simplified example, say I have a class called apple() and one called orange(), and I have an array that stores many instances of both apples and oranges. There's no way to predict whether element N is an apple or an orange until it's looked at directly.

    If I'm iterating through this array with a simple foreach loop, and I want to make a brand new instance of the same type as each element in the array, how would I do it? I know I can use typeof to get the object name as a string, but that doesn't help me instantiate a new instance of it.

    I'm sure there's a feature of Javascript I'm missing that's the key to this. Can someone help me?
    *~Life is like Javascript: You may have some problems, but if you're not afraid to ask for help, it will always turn out right.~*

  2. #2
    Join Date
    Dec 2012
    Posts
    98
    You can access the objects name through constructor property which contains the name of the object, then you can use the window object to create a new instance of the object

    HTML Code:
    function Apple(colour,radius) {
    	this.colour = colour;
    	this.radius = radius;
    }
    function Orange(colour,radius) {
    	this.colour = colour;
    	this.radius = radius;
    }
    var objects = [];
    for(var i = 1; i < 20; i++) {
    	objects.push(new Apple("red",i));
    	objects.push(new Orange("orange",i));
    }
    for(var i = 0, l = objects.length; i < l; i++) {
    	var newObj = new window[objects[i].constructor.name]("red",5);
    	console.log(newObj);
    }

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    A loop which work on IE9 and show the content of the objects if the console is open (Source Stackoverflow).
    Code:
    for(var i = 0, l = objects.length; i < l; i++) {
        var name=objects[i].constructor.toString().match(/function (\w*)/)[1];
        alert(name)
        var newObj = new window[name]("red",5);
        if (window.console) console.log(JSON.stringify(newObj));
    }
    But it's no way of getting anonymous function names !
    Last edited by 007Julien; 06-29-2013 at 08:19 AM.

  4. #4
    Join Date
    Dec 2003
    Location
    FL
    Posts
    93
    Quote Originally Posted by iBeZi View Post
    You can access the objects name through constructor property which contains the name of the object, then you can use the window object to create a new instance of the object

    HTML Code:
    function Apple(colour,radius) {
    	this.colour = colour;
    	this.radius = radius;
    }
    function Orange(colour,radius) {
    	this.colour = colour;
    	this.radius = radius;
    }
    var objects = [];
    for(var i = 1; i < 20; i++) {
    	objects.push(new Apple("red",i));
    	objects.push(new Orange("orange",i));
    }
    for(var i = 0, l = objects.length; i < l; i++) {
    	var newObj = new window[objects[i].constructor.name]("red",5);
    	console.log(newObj);
    }
    Thank you very much! This worked perfectly . I had no idea the window array contained class name keys like that. I knew I was overlooking something . Thanks again!
    *~Life is like Javascript: You may have some problems, but if you're not afraid to ask for help, it will always turn out right.~*

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles