This question already has answers here:
Change the value of an array changes original array JavaScript
(3 answers)
Closed 6 years ago.
Maybe the question already exists, but I don't find anything because I don't know how to call this problem.
I have an object function and want to assign the object variable into a variable:
this.myFunction = function() {
var selection; // internal variable
selection = this.selection; // both are [0,0]
console.log(this.selection); // result: [0,0]
selection[0] = 500; // set internal variable[0] 500
console.log(selection); // result: [500,0]
console.log(this.selection); // result: [500,0] <-- expected: [0,0]
}
How I can change the local variable without changing the other?
When you call selection = this.selection, you are copying the reference of this.selection value. So when you change the local variable selection, you are changing this.selection too.
You have to use the method slice() to create a new array from the value. Like this:
selection = this.selection.slice();
Related
This question already has answers here:
Assigning prototype methods *inside* the constructor function - why not?
(6 answers)
Closed 5 years ago.
I have writtern this two different ways and both giving me same result , so which I can use when ?
FIRST EXAMPLE
var BaseCls = function() {
BaseCls.prototype.name = "John";
};
var JustCls = new BaseCls();
console.log(JustCls.name); // This is giving result John
SECOND EXAMPLE
var BaseCls = function() {};
BaseCls.prototype.name = "John";
var JustCls = new BaseCls();
console.log(JustCls.name); // This is also giving result John
Both giving me same result so I just want to know is there any other criteria which lead to write this property / method with prototype inside / outside main function ?
Thanks for consideration
You should change prototype only outside the constructor.
Otherwise you change it every time you create an instance.
This question already has answers here:
Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop
(25 answers)
Closed 5 years ago.
I need one clarification java script. I used two variables in my program.
I stored particular array in first variable and again create one variable.
I have stored second variable value in first variable.Then I pushed one vale in second variable... If i print first variable means second variable vale displayed..
My expectation is first variable value don't want to change..
// first variable
var test = ["a","b","c"];
// second variable
var arr = test;
arr.unshift("d");
// second variable print
console.log(arr); // ["a","b","c","d"]
// First variable print
console.log(test); // ["a","b","c","d"]
// I want to maintain first original value
// Expectation result
console.log(test); // ["a","b","c"]
I need maintain original value in first variable..What I do?
You can use the spread operator (check browser capability)
var arr = [...test];
// first variable
var test = ["a","b","c"];
// second variable
var arr = [...test];
arr.push("d");
// second variable print
console.log(arr); // ["a","b","c","d"]
// First variable print
console.log(test); // ["a","b","c","d"]
// I want to maintain first original value
// Expectation result
console.log(test); // ["a","b","c"]
This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I am new to JavaScript objects so please bear with me.
This is my JavaScript:
var dragSources = {
elementSources: {
squareSource: document.getElementById('squareSource'),
circleSource: document.getElementById('circleSource')
},
ifDragSource: function(elem) {
console.log(this.elementSources.length);
}
};
If you look at console.log(this.elementSources.length); you can probably tell that I am trying to get the length of the elementSources property. However, it returns undefined. What am I doing wrong?
It's an object, not an array, therefore it doesn't have a length property.
You could use Object.keys(this.elementSources).length to get the number of keys.
The .keys() method essentially returns an array of the object's keys.
In this case, Object.keys(this.elementSources) returns:
["squareSource", "circleSource"]
Then we are just getting the length of that array, which is 2.
var dragSources = {
elementSources: {
squareSource: document.getElementById('squareSource'),
circleSource: document.getElementById('circleSource')
},
ifDragSource: function(elem) {
console.log(Object.keys(this.elementSources).length);
}
};
dragSources.ifDragSource(); // 2
Technically if you need the length property in a code you have to store the length value directly in the object.
Using Object.key function decreases the code efficiency .
every time you invoke that function to access that value you have to re-run the same function again and again.
in order access to the length property in a js array, the BIG O is always equal to 1.
because when you update the array the length property would be updated consequently
But in that case the big O would be in the size of the Object and (O) = n
This question already has an answer here:
Changing variable in another function in JavaScript
(1 answer)
Closed 7 years ago.
I am trying to automate the changing of a variable through a function. However although it returns the right value, it doesn't change the actual value of the variable passed to it.
function change(one, two){
one = two;
return one;
}
var test = 1;
change(test, 5);
// returns 5;
console.log(test);
// still 1
Why does this happen and how can I solve this?
You can't pass variables. When you call change(test, 5); you are passing the value of test not the variable test.
That value is copied to the variable one.
You then assign a new value to one, but that doesn't touch test.
If you want to do anything like this, you need to pass an object and then modify the value of a property of the object.
function change(one, two){
one.test = two;
return one.test;
}
var myObject = {
test: 1
};
change(myObject, 5);
console.log(myObject.test);
You can't do this in JavaScript. Variables cannot be passed by reference, they are always passed by value.
This question already has an answer here:
Accessing a JSON property (String) using a variable
(1 answer)
Closed 8 years ago.
I want to use a string as a JSON property in JavaScript.
var knights = {
'phrases': 'Ni!'
};
var x = 'phrases';
console.log(knights.x); // Doesn't log "Ni!"
When I run this code, it obviously doesn't work because it interprets "x" and not the contents of the variable "x".
The full code in context on pastebin: http://pastebin.com/bMQJ9EDf
Is there an easy solution to this?
knights.x looks for a property named x. You want knights[x], which is equivalent to knights['phrases'] == knights.phrases.
Full code (fixing a couple of typos in your example):
var knights = {
"phrases": "Ni!"
};
var x = 'phrases';
console.log(knights[x]); // logs Ni!
Try this to access using variables having string values
kinghts[x]
Basically this is trick
kinghts[x]==knighted["phrases"]==knighted.phrases.
knights.x will get a key named x, So it'll return undefined here.
knights.x is the same as knights['x'] - retrieving a property under the key x. It's not accessing the variable x and substituting in the value. Instead, you want knights[x] which is the equivalent of knights['phrases']