Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've come across this problem a few times. I usually design Javascript classes slightly differently, and have never experienced scoping issues with this method.</p> <p>The below example does what you're wanting (with some dummy data, in a dummy Ajax request and loop to cater for scope issues). It also creates two objects with randomised data to test that they're not being stored statically in the prototype. Timeout is to ensure that the Ajax request has completed before outputting data. <a href="http://jsfiddle.net/wERTp/16/" rel="nofollow">http://jsfiddle.net/wERTp/16/</a></p> <pre><code>var Container = function() { }; Container.prototype = { width: '', height: '', margin: '', margin_option: '', position: '', readXML4Container: function() { $.get("", this.saveValues.bind(this)); }, saveValues: function(xml) { var self = this; var values = [{ width: 10 + Math.random(), height: 10 + Math.random(), margin: 10 + Math.random(), margin_option: '', position: 'relative' }]; $.each(values, function() { self.width = this.width; self.height = this.height; self.margin = this.margin; self.margin_option = this.margin_option; self.position = this.position; }); }, outputValues: function() { console.log(this.width, this.height, this.margin, this.margin_option, this.position); } } var container = new Container(); container.readXML4Container(); setTimeout(function() { container.outputValues(); }, 1000); var container2 = new Container(); container2.readXML4Container(); setTimeout(function() { container2.outputValues(); }, 1000);​ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload