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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:03:15+00:00 2026-05-16T18:03:15+00:00

I am having troubles using shift() to call functions I have put in an

  • 0

I am having troubles using shift() to call functions I have put in an array. I have put together a simple example illustrating the problem.

Essentially the function gets called, however, the changes to variables in the function do not stick.

<html>
<head>
<script type="text/javascript">
Taco = function() {};
Taco.prototype.init = function() {
  this.ex1 = "ex1 in init()";
  alert(this.ex1);
};
</script>
</head>
<body>
<input type="Submit" onClick="withShift();" value="withShift"/>
<div id="div1">
</div>
<input type="Submit" onClick="noShift();" value="noShift"/>
<div id="div2">
</div>
<script type="text/javascript">
// This calls init but does not hold the value of ex1 after the call
withShift = function() {
taco = new Taco();
  funcQ = [];
  funcQ.push(taco.init);
  funcQ.shift()(); 

  div1 = document.getElementById("div1")
  div1.appendChild(document.createTextNode(taco.ex1));
};

// this calls init and it holds the value...
noShift = function() {
  taco2 = new Taco();
  taco2.init();
  div1 = document.getElementById("div2")
  div1.appendChild(document.createTextNode(taco2.ex1));
}
</script>
</body>
</html>

Thank you in advance for any suggestions.

  • 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-16T18:03:15+00:00Added an answer on May 16, 2026 at 6:03 pm

    JavaScript does not remember the this argument when you pass method pointers. You have to use the call or apply method on the function object to explicitly pass this.

    Using taco.init is roughly the same as using Taco.prototype.init when it comes to passing function pointers. Here’s what would be the working way:

    taco = new Taco();
    funcQ = [];
    funcQ.push(taco.init);
    // pass taco first, and the non-hidden function arguments after;
    // in this case, no other argument
    funcQ.shift().call(taco);
    

    If you can’t use this kind of syntax, you may use anonymous functions:

    taco = new Taco();
    funcQ = [];
    funcQ.push(function() { taco.init(); });
    funcQ.shift()();
    

    In opposition to the object.method syntax that doesn’t carry the this argument, closures are reliable.

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

Sidebar

Related Questions

Im having trouble using a .NET COM in vb6, It compiles ok and I
I am using mssql and am having trouble using a subquery. The real query
I am having trouble using the attribute XPath Selector in ElementTree, which I should
I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically
I'm new to development using Xcode, and am having trouble using the built-in Leaks
I am using NHibernate and Rhinomocks and having trouble testing what I want. I
I'm having trouble reading a chunked response when using a StreamReader to read the
I'm having trouble with redirecting urls using the .htaccess file. This is what my
I am having trouble deleting orphan nodes using JPA with the following mapping @OneToMany
We're having trouble setting window captions using cyrillic or japanese characters. We either see

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.