Javascript Need a basic syntax explaination [duplicate] - javascript

This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 2 years ago.
var x = 5;
function test2(x) {
x = 7;
}
test2(8);
alert(x);
Why exactly is this putting out the global var x=5, without being affected by anything within the function.

Because you passed a param called x, which is the same name as your global var. Try this out:
var x = 5;
function test2(y) {
x = y;
}
test2(8);
alert(x);

Related

Javascript (function (){})() closure [duplicate]

This question already has answers here:
Javascript How to define multiple variables on a single line?
(10 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 2 months ago.
Can any one explain why this happens?
// Example 1
(function () {
var a = b = 5;
})();
console.log(b); // Output 5
var a = b = 5 and var b = 5 is totally different?

Is it possible in Javascript to get the name of a variable? [duplicate]

This question already has answers here:
Determine original name of variable after its passed to a function
(9 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 7 years ago.
I know this is completely useless, but I'm nonetheless curious. Is it possible to do something like the below?
function getVariableName ( v )
{
// .... do something .....
// return the string "v"
}
var x = 69;
var y = "somestring";
console.log(getVariableName(x)); // prints "x" to the console
console.log(getVariableName(y)); // prints "y" to the console
function getArgNames(fn) {
var matches = fn.toString().match(/\(([a-z_, ]+)\)/i);
if (matches.length > 1) {
return matches[1].replace(/\s+/g, '').split(',');
}
return [];
}
That will return an array of the names of all arguments passed in to the function.

Store value in the given variable through functions [duplicate]

This question already has answers here:
Pass variables by reference in JavaScript
(16 answers)
Pointers in JavaScript?
(15 answers)
Closed 7 years ago.
Is there any how javascript or jquery to store values in the given variables while interacting through functions wuthout using global vars?
I made an example: http://jsfiddle.net/1fw2urks/3/
$(document).ready(function(){
start();
});
function start(){
var n = 0;
for (i = 0; i < 10; i++){
count(n);
}
alert(n);
}
function count(n){
countAgain(n);
}
function countAgain(n){
n += 1;
}
n var should come with value 10 if it worked, but it seems that we can't change the passed parameters value while in the function.
Is this affirmation right?
(I did some research and i found this answer, but, I am stubborn)

How can I safely use variables in objects? [duplicate]

This question already has answers here:
dynamic keys for object literals in Javascript [duplicate]
(8 answers)
in javascript is it possible to construct an object literal with expressions evaluating to strings for property names? [duplicate]
(2 answers)
variable in javascript statement
(2 answers)
Closed 8 years ago.
I'm bored and messing with my console and I came up with the following code:
I was trying something like:
x = 16;
y = 26;
f = { x+y:"String!"};
expecting something or somehow to do:
Object {1626: "String!"}
Or at least
Object {42: "String!"}
I ended up with
x = 16;
y = 26;
eval("f = { "+x+y+":\"String!\"}");
Which returned as expected:
Object {1626: "String!"}
I've always been told to avoid eval() and never even think about using it due to something about security. Is there a way to use variables in declaring the property (Sorry if I don't know what it's called exactly)
x = 16;
y = 26;
f = {};
f[x+''+y] = "String!"; // For f[1626]
or
f[x+y] = "String!" // For f[42]
I made this exact same mistake when I started learning JavaScript: JavaScript set object key by variable
You first must make the object:
var f = {};
Then you can use variables to dynamically create keys:
var x = 16, y = 26;
f[x+y] = "Integer";
f[x.toString() + y.toString()] = "String";

local and global scope coffeescript [duplicate]

This question already has answers here:
How do I define global variables in CoffeeScript?
(9 answers)
Closed 10 years ago.
With javascript:
function myFunc() {
var x = 5;
};
console.log(x);
I get //undefined and with:
function myFunc() {
x = 5;
};
console.log(x);
I get 5
With coffeescript this variable var x = 5; is x = 5.
For example this is possible?:
myFunc ->
window.x = 5;
console.log window.x
Instead of:
myFunc ->
x = 5;
console.log x
My question is How I can differentiate a global variable of a local variable with CoffeeScript?
for global scope you should use functions like this:
myFunc = =>
#x = 5;
myFunc()
console.log x
example of generated code:
http://jsfiddle.net/Upward/wZ7w4/

Categories