Javascript declaration [closed] - javascript

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
Hi this is a simple question. I was wondering is there any different when you declare something like this. Thanks
selectedData[key](val)
and
selectedData[key] = val

This line selectedData[key](val) is not a declaration, it's calling the function that is stored under the key key in the object selectedData and it's passing the parameter val to that function.
The other line selectedData[key] = val is assigning the value val to the key key in the object selectedData.

In the first case, you're calling whatever is in selectedData[key] as a function with val as an argument while in the second one you're assigning it.

Related

How to set variable of image src and use in javascript function? [closed]

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 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
So if i have some codes like this
If(sceneA){document.getElementById("divA").src="A.png";}
If(document.getElementById("divC")=="A.png"){document.getElementById("divB").src="B.png";}//And alot of codes here
How can I use a var to shorten strings like
document.getElementById("divA").src="A.png"
document.getElementById("divB").src="B.png"
so i can use them both inside (IF) and {function}
As they have to repeat quite a few times in the codes.
Thanks alot!
That's exactly what functions are for.
Define a function for repeating blocks of code as follows:
function setSrc(divName, imgName){
document.getElementById("div"+divName).src = imgName+".png";
}
function srcEquals(divName, imgName){
return (document.getElementById("div"+divName).src == imgName+".png");
}
Then you can replace your code with this:
if(sceneA){setSrc("A","A");}
if(srcEquals("C", "A")){setSrc("B","B");}

Rule of Naming Variables for Boolean [closed]

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 3 years ago.
Improve this question
how should I name for variables on type Boolean like 'Mom is away from keyboard'.
var momIsAwayFromKeyboard = mom.isAwayFromKeyborad;
is correct??
You could use a destructuring assignment:
let {isAwayFromKeyboard} = mom;
In this way you need only to write the variable name once and there will no difference b/w key and variable name.
let mom = {
isAwayFromKeyborad:true
}
let {isAwayFromKeyborad} = mom;
console.log(isAwayFromKeyborad)
Usually, you start with 'is'.
I think a good, short, descriptive name could be:
isMomAFK
or
isMomAway
Think of it as a true/false question: 'Is Mom away from keyboard?'

Remove an array-Javascript [closed]

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 have an array like this:
var animals=["cat","dog","snake","rhino"];
But sometimes I have to delete this array(remove it from the dom).
I have tried animals.remove; and $(animals).remove() and animals.remove() but none of them did the trick.Any ideas?
var animals=["cat","dog","snake","rhino"];
then to clear it do:
animals=[];
or
animals.length=0;
or
while(animals.length > 0) {
animals.pop();
}
Just assign the animals array to a value undefined and the array data will be dereferenced and garbage collected.
Donot try to call delete operator that is explicit removal.
animals = undefined
OR
animals = void 0
Thanks
Just Clear The Array
Using This 2 Methods
animals.length =0
animals=[];

jQuery filter options with value greater (less) than variable [closed]

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
I need to disable values of options in select that are less than some number, which I do not know beforehand (it is set in another form element). I need something like code below, but with variable value of "variableInteger":
select.children().filter(function() {
return $(this).attr("value") > variableInteger;
}).each(function () {$(this).attr('disabled', 'disabled')});
Is there some clever way to do it?
PS. variableInteger value is from another form element, which name is also known only at runtime.
No need for the .each, also make use of prop and this.value (no need for $(this).attr("value");)
select.children("option").filter(function() {
return this.value > variableInteger;
}).prop("disabled", true);

javascript return, not functioning [closed]

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 have this code:
replaceAny('this','that',string);
if(val!="")
the replaceAny function looks like this:
function replaceAny(first,second,ele) {
var val = ele.replace(first,second);
alert(val);
return val;
}
But then after running the replaceAny function (and the alert shows the right value, the if condition tells me that the variable val is not set, why?!
You are not using the return value from the function correctly. This is something like what you want
function replaceAny(first,second,ele) {
return ele.replace(first,second);
}
var val = replaceAny('this','that',string);
if(val!=""){
//something, something darkside
}
I think you are not assigning the value returned by your function to your variable val.
Try like this
val = replaceAny('this','that',string);
if(val!="")

Categories

Resources