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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:26:38+00:00 2026-06-01T04:26:38+00:00

When I try to use this in my Javascript prototype like so: Array.prototype.sample =

  • 0

When I try to use this in my Javascript prototype like so:

Array.prototype.sample = function() {
  return this[Math.floor (Math.random() * this.length )];
}

As well as implement my tests (Jasmine):

describe('sample()', function() {
  it('returns a random item of an array', function() {
    orig_array = ['foo', 'bar', 'baz', 'qux'];
    sampled_word = orig_array.sample(); 
    expect(orig_array).toContain(sampled_word);
  });
});

My test fails. These methods were originally functions using arguments to process the this keyword inside of the prototype, but due to the fact that this is going to be in a small Javascript library, I’d rather implement it as a prototype. Is the this keyword correct in this context, or is there a error with the prototype I’m not getting? Thanks.

  • 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-01T04:26:39+00:00Added an answer on June 1, 2026 at 4:26 am

    The problem is in this part of the code.

    Array.prototype.sample = function() {
       return this[Math.floor (Math.random() * array.length )];
    }
    

    Simply ‘array’ is not defined. The code which should work is

    Array.prototype.sample = function() {
       return this[Math.floor (Math.random() * this.length )];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to update my profile image and use this code: $connection->post('account/update_profile_image', array('image' =>
i use this javascript to capture all checkboxes checked in a flexigrid and try
I try to use this mapping : @Entity @Table(name=ecc.\RATE\) @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=DISCRIMINATOR, discriminatorType= DiscriminatorType.STRING) public
I get this error when I try to use time_ago_in_words : Comparison of String
I try to use the boolean in my class and get this message reference
i am having this problem try to use the cocos2d CCScrollLayer I have added
I have this implementation with fusion table layers where I try to use the
Try to download this page: http://www.4shared.com/get/B4AgMkcw/cookies.html I can use wget/firefox to get the whole
This article on MSDN states that you can use as many try catch blocks
I'm trying to convert this Javascript code: self.userSerialEvent = function (join, value, tokens) {

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.