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 6568427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:28:34+00:00 2026-05-25T14:28:34+00:00

In the CoffeeScript program below, I create a subclass of Array which sets two

  • 0

In the CoffeeScript program below, I create a subclass of Array which sets two positions in its constructor:

class SetPositionsArray extends Array
  constructor: (x,y) ->
    @[0] = x
    @[1] = y

  myLength: ->
    @length

sp_array = new SetPositionsArray 1, 2

console.log "sp_array: "
console.log sp_array
console.log "sp_array[0]: "
console.log sp_array[0]
console.log "sp_array[1]: "
console.log sp_array[1]
console.log "sp_array.length: "
console.log sp_array.length
console.log "sp_array.myLength(): "
console.log sp_array.myLength()

I would hope that this code would change the length property of sp_array, since it effectively sets positions on it. However, the output I get is:

$ coffee sp.coffee
sp_array: 
[ 1, 2 ]
sp_array[0]: 
1
sp_array[1]: 
2
sp_array.length: 
0
sp_array.myLength(): 
0

That is, the length is 0.

Then, I created another class which pushes values in the instance instead of setting them:

class PushValuesArray extends Array
  constructor: (x,y) ->
    @push x
    @push y

  myLength: ->
    @length


pv_array = new PushValuesArray 1, 2


console.log "pv_array: "
console.log pv_array
console.log "pv_array[0]: "
console.log pv_array[0]
console.log "pv_array[1]: "
console.log pv_array[1]
console.log "pv_array.length: "
console.log pv_array.length
console.log "pv_array.myLength(): "
console.log pv_array.myLength()

In this case, I get the expected result, except that there is an actual length attribute in the array (while I would imagine that it would be some internal detail):

$ coffee pv.coffee
pv_array: 
[ 1, 2, length: 2 ]
pv_array[0]: 
1
pv_array[1]: 
2
pv_array.length: 
2
pv_array.myLength(): 
2

So, why does setting the position in the array does not change its length?

This question is related to this one for which I posted this answer.

  • 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-05-25T14:28:34+00:00Added an answer on May 25, 2026 at 2:28 pm

    The simplest explanation is: length is magic.

    length obviously doesn’t behave like an ordinary property, since it changes its value when you insert/delete other properties on its object (and, conversely, setting length = 0 will delete other properties); but there’s nothing special about the identifier “length”. That means you can easily write foo.length = 'bar', and the world will keep turning. Only on arrays does it have its special nature.

    Now, you might expect when when you extend the Array constructor, that you get an array—but do you? Well, in one sense you do:

    class PushValuesArray extends Array
    (new PushValuesArray) instanceof Array  # true
    

    Unfortunately, for the purpose of length, you don’t. All the extends keyword does here is create a prototype chain, and the Array prototype has a distinctly non-magical length property:

    Array::length  # 0
    

    That’s all you get on your PushValuesArray instance. Sadly, there’s no way to duplicate that length magic on your own objects. Your only option is to either write a function instead (say, size()), or modify the Array prototype with the methods you want and use true arrays instead.

    To sum up: Subclassing Array won’t get you very far. That’s why, for instance, jQuery has a very array-like API on its objects—

    $('body').length  # 1
    $('body')[0]      # [object HTMLBodyElement]
    

    —but doesn’t actually make those objects inherit from the Array prototype:

    $('body') instanceof Array  # false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following coffeescript code: class Animal constructor: (@name) -> speak: (things) -> My
I've the following two CoffeeScript class definitions. I expected them to the same behavior,
I have three coffeescript classes, set up like this: class A class C extends
Coffeescript looks pretty cool. Has anyone used it? What are its Pros & Cons?
From googling/forums I think there could be two issues, neither of which I know
Trying to understand how CoffeeScript instance and class variable works I came with this
I am trying to create or find a CoffeeScript implementation of the Levenshtein Distance
I'm trying to find an elegant way in Coffeescript to merge an array of
CoffeeScript list comprehensions are slightly different from Pythons... which of these is the way
How would you extend a class using CoffeeScript, but have the construction arguments passed

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.