Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:27:29+00:00 2026-06-19T03:27:29+00:00

I’d like to set the name of a JavaScript pseudoclass stored in an array

  • 0

I’d like to set the name of a JavaScript pseudoclass stored in an array with a specific name, for example, the non-array version works flawlessly:

var Working = new Array();
Working = new Function('arg', 'this.Arg = arg;');
Working.prototype.constructor = Working;
var instw = new Working('test');
document.write(instw.Arg);
document.write('<BR />');
document.write((instw instanceof Working).toString());

Output:

test
true

However this format does not function:

// desired name of pseudoclass
var oname = 'Obj';

var objs = new Array();
objs.push(new Function('arg', 'this.Arg = arg;'));

// set objs[0] name - DOESN'T WORK
objs[0].prototype.constructor = oname;

// create new instance of objs[0] - works
var inst = new objs[0]('test');
document.write(inst.Arg);

document.write('<BR />Redundant: ');
// check inst name - this one doesn't need to work
try { document.write((inst instanceof objs[0]).toString()); } catch (ex) { document.write('false'); }
document.write('<BR />Required: ');
// check inst name - this is the desired use of instanceof
try { document.write((inst instanceof Obj).toString()); } catch (ex) { document.write('false'); }

Output:

test
Redundant: true
Required: false

Link to JSFiddle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:27:30+00:00Added an answer on June 19, 2026 at 3:27 am

    You’ve got a couple of things going on here that are a little bit off-kilter in terms of JS fluency (that’s okay, my C# is pretty hackneyed as soon as I leave the base language features of 4.0).

    First, might I suggest avoiding document.write at all costs?
    There are technical reasons for it, and browsers try hard to circumvent them these days, but it’s still about as bad an idea as to put alert() everywhere (including iterations).
    And we all know how annoying Windows system-message pop-ups can be.

    If you’re in Chrome, hit CTRL+Shift+J and you’ll get a handy console, which you can console.log() results into (even objects/arrays/functions), which will return traversable nodes for data-set/DOM objects and strings for other types (like functions).
    One of the best features of JS these days is the fact that your IDE is sitting in your browser.
    Writing from scratch and saving .js files isn’t particularly simple from the console, but testing/debugging couldn’t be any easier.

    Now, onto the real issues.

    Look at what you’re doing with example #1.
    The rewriting of .prototype.constructor should be wholly unnecessary, unless there are some edge-case browsers/engines out there.

    Inside of any function used as a constructor (ie: called with new), the function is basically creating a new object {}, assigning it to this, setting this.__proto__ = arguments.callee.prototype, and setting this.__proto__.constructor = arguments.callee, where arguments.callee === function.

    var Working = function () {};
    var working = new Working();
    console.log(working instanceof Working); // [log:] true
    

    Working isn’t a string: you’ve make it a function.
    Actually, in JS, it’s also a property of window (in the browser, that is).

    window.Working    === Working;  // true
    window["Working"] === Working; // true
    

    That last one is really the key to solving the dilemma in example #2.

    Just before looking at #2, though, a caveat:
    If you’re doing heavy pseud-subclassing,

    var Shape = function () {
        this.get_area = function () { };
    },
    
    Square = function (w) {
        this.w = w;
        Shape.call(this);
    };
    

    If you want your instanceof to work with both Square and Shape, then you have to start playing with prototypes and/or constructors, depending on what, exactly, you’d like to inherit and how.

    var Shape = function () {};
    Shape.prototype.getArea = function () { return this.length * this.width; };
    
    var Square = function (l) { this.length = l; this.width = l; };
    Square.prototype = new Shape();
    
    var Circle = function (r) { this.radius = r; };
    
    Circle.prototype = new Shape();
    Circle.prototype.getArea = function () { return 2 * Math.PI * this.radius; };
    
    var circle = new Circle(4),
        square = new Square(4);
    
    circle instanceof Shape; // true
    square instanceof Shape; // true
    

    This is simply because we’re setting the prototype object (reused by every single instance) to a brand-new instance of the parent-class. We could even share that single-instance among all child-classes.

    var shape = new Shape();
    Circle.prototype = shape;
    Square.prototype = shape;
    

    …just don’t override .getArea, because prototypical-inheritance is like inheriting public-static methods.

    shape, of course, has shape.__proto__.constructor === Shape, much as square.__proto__.constructor === Square. Doing an instanceof just recurses up through the __proto__ links, checking to see if the functions match the one given.

    And if you’re building functions in the fashion listed above (Circle.prototype = new Shape(); Circle.prototype.getArea = function () { /* overriding Shape().getArea() */};, then circle instanceof Circle && circle instanceof Shape will take care of itself.

    Mix-in inheritance, or pseudo-constructors (which return objects which aren’t this, etc) require constructor mangling, to get those checks to work.

    …anyway… On to #2:

    Knowing all of the above, this should be pretty quick to fix.

    You’re creating a string for the desired name of a function, rather than creating a function, itself, and there is no Obj variable defined, so you’re getting a “Reference Error”: instead, make your “desired-name” a property of an object.

    var classes = {},
        class_name = "Circle",
    
        constructors = [];
    
    
    classes[class_name] = function (r) { this.radius = r; };
    
    constructors.push(classes.Circle);
    
    var circle = new constructors[0](8);
    circle instanceof classes.Circle;
    

    Now everything is nicely defined, you’re not overwriting anything you don’t need to overwrite, you can still subclass and override members of the .prototype, and you can still do instanceof in a procedural way (assigning data.name as a property of an object, and setting its value to new Function(data.args, data.constructor), and using that object-property for lookups).

    Hope any/all of this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.