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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:21:03+00:00 2026-05-31T07:21:03+00:00

Why can I do this: Array.prototype.foo = function() { this.splice(0, this.length); return this.concat([1,2,3]); }

  • 0

Why can I do this:

Array.prototype.foo = function() {
    this.splice(0, this.length);
    return this.concat([1,2,3]);
}

But I can’t do this:

Array.prototype.foo = function() {
    return this = [1,2,3];
}

Both functions destroy the value of this and change it to [1,2,3] but the second one throws the following error: Uncaught ReferenceError: Invalid left-hand side in assignment

I suspect it’s because allowing assignment means I could potentially change the array to something else (like a string), but I’m hoping someone out there knows for sure and/or has a more detailed explanation.

  • 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-31T07:21:05+00:00Added an answer on May 31, 2026 at 7:21 am

    It’s not permitted to assign a value to this within a function. Suppose that you could do this, and your code looked something like:

    Array.prototype.foo = function() {
        return this = [1, 2, 3];
    }
    
    var a = ["beans", "rice"];
    a.foo();
    // a now points to an object containing [1, 2, 3]
    

    Now, what if you did this:

    var a = ["beans", "rice"];
    var b = a; // b refers to the same object as a
    b.foo();
    // what does b refer to now? how about a?
    

    The act of calling a function .foo() on an object should not change the identity of the object. This would be very confusing for the caller if b suddenly started referring to a different object than a simply because some method was called.

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

Sidebar

Related Questions

I need a prototype done in this way: Array.prototype.getname=function(){ [...]return arrayname; } So I
I added unique() function to Javascript Array: Array.prototype.unique = function(){ return this.filter(function(item, ind, arr){
Is there a way I can get this array walk with my anonymous function
How can I sort this array by the value of the "order" key? Even
I got this from Paul Irish , window.log = function(){ if(this.console){ console.log( Array.prototype.slice.call(arguments) );
how can i get [user_id] from this array? Array ( [2] => cbpaidSubscription Object
how i can duplicate one element from array: for example, i have this array:
I need to extract Asia from this array. How can i do that. Here
If you have a string of 1,2,3,1,5,7 you can put this in an array
how can i convert this into an array? if someone searches for lo he

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.