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

  • Home
  • SEARCH
  • 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 8618149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:02:15+00:00 2026-06-12T06:02:15+00:00

In my investigation into making Array-like objects, I made this function, Array2 = function(){

  • 0

In my investigation into making Array-like objects, I made this function,

Array2 = function(){
    var out = [];
    Object.defineProperty(out, 'prototype', { value : Array2.prototype }); // store a reference
    out.__proto__ = Array2.prototype; // necessary as Array uses __proto__ and not prototype

    if(arguments.length > 1) Array.prototype.push.apply(out, arguments);   // re-implement constructor's
    else if(arguments.length === 1) out.length = arguments[0];             // argument handling behaviour

    return out;
};

// allow for normal prototyping behaviour
Array2.prototype = [];
Object.defineProperty(Array2.prototype, 'constructor', { value : Array2 });

and noticed that calling Array2() was returning the same as calling new Array2(), which isn’t what I was expecting, so I considered a similar function for integers

Int = function(n){
    var out = ~~n;
    out.prototype = Int.prototype;
    out.__proto__ = Int.prototype;

    this.value = out; // added to check value when working as object

    return out;
};

Int.prototype = 0;
Int.prototype.constructor = Int;

this time, Int returns a normal instance of a Number (__proto__ and prototype as for any number literal) and new Int returns an “Int” Object with Empty as __proto__ and undefined for prototype, with the number available through .value, same as calling without new.

Why are these very similar functions acting so differently, and why is new resulting in the for the first one? It is most likely something obvious I’ve overlooked.
Only tested in Google Chrome.

  • 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-12T06:02:17+00:00Added an answer on June 12, 2026 at 6:02 am

    Actually, your Array2 function return real Arrays instead of only Array-like objects, this does not change when setting the [[prototype]] to an object that inherits from Array.prototype (altough you should not have created an array using [], but a plain object using Object.create(Array.prototype).

    Your function Int has several problems.

    out is a primitive number value, and has no properties. When assigning some, it will be implicitly casted to a Number object, which is discarded rightafter. The same problem with the “constructor” property on Int.prototype = 0.

    Also, you can’t use primitive values like 0 as prototype objects. When creating a new Int instance, it will inherit from the default Object.prototype as 0 is not of type “object”. I’m not sure what happens when assigning such to the non-standard __proto__ property, but I guess it just fails.

    Use this instead:

    function Int(n){
        var out = ~~n;
        this.valueOf = function(){ return out; };
        return out; // when not used as a constructor, return int-casted number
    };
    
    Int.prototype = Object.create(Number.prototype, {
        constructor:{value:Int}
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am investigatin this field to obtain object detection in real time. Video example:
I'm doing some investigation into options for an app I am building. Do browser
How to output a particular JQuery array into a particular Div. For example I
I am looking into a bug in a site I've inherited, and my investigation
I have a script and it has run into some problems: (function($) { $.fn.extend({
After intense levels of investigation into fixed table headers I stumbled across a fairly
I have recently been doing a bit of investigation into the different types of
As part of an investigation into enterprise level server side document generation I have
this is my code snippet, where the program doesn't enter the foreach loop: var
I am having issues while passing objects across Appdomains.During further investigation I found that

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.