How to when click “Create” button add last state of object to array and create new clean ready to continue, and if click “Continue” button modyfy only actual object, now is modified all objects in sections array?
Illustrative materials:
HTML:
<button onclick="create()">Create</button>
<button onclick="add()">Continue</button>
JavaScript:
var sections = [];
create = function() {
sections.push(section);
section.array = [];
section.c = 0;
section.add();
$("body").append("<br>Add to array at moment last state of object and make new one<br>" + JSON.stringify(sections) + "<br >");
}
add = function() {
section.add();
$("body").append("<br>continue only this object<br>" + JSON.stringify(sections) + "<br >");
}
var section = {
array: [],
c: 0,
add: function() {
section.c++;
section.array.push(section.c);
}
}
You will need to create new section objects, instead of resetting the properties of your one
sectionvariable (in yourcreatefunction):Yet, I’d say this is a case for a constructor function:
and then use
new Section()instead ofmakeSection().