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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:10:20+00:00 2026-05-30T10:10:20+00:00

I’ve been experimenting with Element storage, and I’m stumped by a problem with .retrieve();

  • 0

I’ve been experimenting with Element storage, and I’m stumped by a problem with .retrieve(); in Element Storage.

The documentation states:

Element:retrieve actually accepts an optional second parameter which will act as the default value to store if another value doesn’t previously exist. It will then retrieve the value as expected.

Basic examples work, but when I am instantiating a Class in the second parameter, that instantiation gets run every time, even though I can verify that .retrieve(); is returning the Class object that was instantiated prior.

Relevent jsFiddle

If you take a look at the fiddle, you can see that every time you click the div, myClass gets instantiated again. Ideally, it should be instantiated once, that object should be saved into Element storage, and retrieves properly in order to stop it from being instantiated again.

This is how I am calling .retrieve();:

$('baz').addEvent('click', function() {
    this.retrieve('foobar', new myClass()).foo();
});

… and just for fun, I tried this (it also didn’t work):

$('baz').addEvent('click', function() {
    this.retrieve('foobar', (function() {
        return new myClass();
    })()).foo();
});

… and here is how I finally got it to work, by NOT using .retrieve()‘s second parameter:

$('baz').addEvent('click', function() {
    var instClass = this.retrieve('foobar');
    if (instClass == null) {
        instClass = this.store('foobar', new myClass()).retrieve('foobar');
    }
    instClass.foo();
});

Any clue what’s causing this behaviour?

  • 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-30T10:10:22+00:00Added an answer on May 30, 2026 at 10:10 am

    The second argument always gets evaluated, yes. In your case, a new instance of myClass is instantiated with every retrieve call – that’s what you see in the console. This will always happen, regardless of the outcome of the call or whether the parameter is actually processed in the method (you could pass e.g. 10 more parameters to the method, they will all get evaluated at the moment of the call).

    In your case, there is another misunderstanding: when the default parameter of the retrieve call is used, it won’t be stored automatically. Therefore your third variant is actually the right solution to the task

    Retrieve value for key ‘foobar’ – if no value has been set, create a new
    instance of
    myClass, store it as ‘foobar’ and return it.

    (Updated – I was wrong with my second state statement, as @Julian H. Lam pointed out: the second parameter to retrieve is actually immediately stored.)

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.