Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So if I make a class, and then create new instances of that class without naming them -maybe with a loop that creates a bunch of instances- how can I call a specific (or unspecific) instance? For example, if I'm generating a bunch a squares, but I want to move a specific one somewhere, how would I do that?
Sorry if this is a total noob question, or if I miss-used some terminology, but I'm pretty new to programming.
Example code:
function example(x){
this.x = x;
}
for(var i=0; i<10; i++){
new example(1);
}
//now how would I get a specific instance of examples to have x = say, 10.
You could put each square in an array and access them that way:
function Square(i){
this.index = i;
}
Square.prototype = {
constructor: Square,
intro: function(){
console.log("I'm square number "+this.index);
}
}
var squares = [];
for(var i = 0;i < 10;i++){
squares.push(new Square(i));
}
squares.forEach(function(square){
// do something with each square
square.intro();
});
Demo: http://jsfiddle.net/louisbros/MpcrT/1/
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to clone a section on a HTML page on user click.
In a form, I'm trying to copy and add a field group, when user wants to add more elements to the form.
While cloning, I want to make sure all the ids are rewritten so that the page and elements are intact.
I've been thinking about a neat way of doing this.
Trying the following approaches.
Am more in favour of JQuery because of the compactness and beauty in the expression.
Would love to hear some perspectives from experts.
JS Approach:
function addElementToParentById(src) {
var temp = document.getElementById(src);
var tempParent = temp.parentElement;
var tempNew = temp.cloneNode(true);
renameIds(tempNew,"1");
tempParent.appendChild(tempNew);
}
function renameIds(param, token) {
if(param.id){
param.id=param.id+token;
}
for(i =0; i < param.childNodes.length; i++) {
var tempChild = param.childNodes[i];
if(tempChild.id) {
tempChild.id = tempChild.id + token;
}
if(tempChild.childNodes
&& tempChild.childNodes.length > 0) {
for(j =0; j < param.childNodes.length; j++) {
renameIds(tempChild.childNodes[j], token);
}
}
}
}
Jquery approach:
$("#addbtnid").click( function() {
$("#filopsmainpane").append($("#filopspane").clone(false).find("*[id]").andSelf().each(
function() { $(this).attr("id", $(this).attr("id") + "1"); }));
}
"Best" is subjective, but I prefer using a framework (for example jQuery) because:
Shorter code is less likely to contain an error and is easier to maintain
The framework's authors have already done the work to deal with browser inconsistencies
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to change constructor base value like I have:
function G()
{
this.speed=1;
}
and var k=new G(); gives me k.speed=1;
now I want that every time I create new G , its speed was like 10;
I tried
G.changeSpeed=function(){this.speed=10;}
G.prototype.changeSpeed=function(){this.speed=10;}
second works on already initialised ones, but first doesn't work at all ( error ).
any way I can do it?
How about:
function G(speed)
{
this.speed = speed;
}
You can do:
var x = new G(1); // x.speed = 1;
var y = new G(2); // y.speed = 2;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So for example if I have this array:
var input = [1,1,2,4,6,7,7,1];
I want the output to be something like:
[1,2,1,4,6,7,1,7]
The order of the new array does not matter, as long as similar items will never (or at least as seldom as possible) neighbor each other.
I can use plain JavaScript as well as underscore.js.
Try the following:
var input = [1,1,2,4,6,7,7,1];
input.sort()
var output = [];
var len = input.length;
for (var i = 0; i < Math.floor((len / 2)); i++) {
output.push(input[i]);
output.push(input[len - i - 1]);
}
if (len % 2) {
var left_over = input[Math.floor(len / 2)];
if (left_over == output[0]) {
output.push(left_over);
} else {
output.unshift(left_over);
}
}
Or see http://jsfiddle.net/d0j3Lfa3/1.
The solution sorts the numbers then alternates high and low. It deals with an odd number of elements, including corner cases such as [1,1,2] and [1,2,2] where it needs to push the middle element differently to pass. Since the input is sorted, input order doesn't affect the output.
This answer may help simplify things a bit.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hello how can I add a second portion to my javascript. Here is the code:
var $pagerT = $('<div class="pager"></div>');
var $pagerB = $('<div class="pager"></div>');
for (var page = 0; page < numPages; page++) {
$('<span class="page-number"></span>').text(page + 1).bind('click', {
newPage: page
}, function(event) {
currentPage = event.data['newPage'];
$table.trigger('repaginate');
$(this).addClass('active').siblings().removeClass('active');
}).appendTo($pagerT).addClass('clickable');
}
Basically I want to add the same class that was added to $pagerT to $pagerB. Here is the code:
}).appendTo($pagerT, $pagerB).addClass('clickable');
Any subjections on how I can process it?
You can use .add
$pagerT.add($pagerB).addClass('clickable');
I'm going to say
var $pagers = $pagerT.add($pagerB);
...
}).appendTo($pagers);
$pagers.addClass('clickable');
The docs say that the argument to appendTo() can be an array of elements, and in that case, "cloned copies of the inserted element will be created for each target after the first".
(However I'm not sure if $pagerT.add($pagerB) creates an array (the doc says it creates a set); or more to the point, whether this value is acceptable as an argument to appendTo(). Testing this is left as an exercise to the reader.)
Or if you value brevity over maintainability,
...
}).appendTo($pagers.addClass('clickable'));
That's assuming that you want to add the <span> to both $pagerT and $pagerB (which you didn't say, but your second code example suggests) and add the 'clickable' class to both (which you said but which conflicts with your original code).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Translated (apparently wrongly) from a C++ book.
If I can get it to work, then I can start trying to understand it.
function recPermute(soFar, rest)
{
if (rest==="")
{
console.log(soFar);
}
else
{
for(i=0; i<rest.length; i++) // <<< error was in not declaring the "i"
{
var next = soFar + rest[i];
var remaining = rest.substr(0,i) + rest.substr(i+1);
recPermute(next, remaining);
}
}
}
function listPerm(s)
{
recPermute("",s);
}
listPerm("kitcap")
You need to declare i so it's scoped to recPermute:
for(var i=0; i<rest.length; i++)
Without the var, it'll be created as a global so each call to recPermute will alter it for any other calls.
for JavaScript, use charAt(), instead of using array like acessing.
var next = soFar + rest.charAt(i);
One thing that could be an issue is you are using effectively the same i for each call to the function. You need to declare a local i or it will be declared in the global scope.
for(var i = 0; ....