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/
Related
This question already has answers here:
What is the temporal dead zone?
(3 answers)
Are variables declared with let or const hoisted?
(7 answers)
Closed 1 year ago.
Why is it logging "Reference Error" in the first case, why didn't it pick it from the global scope like in the second case?
let a = 10;
function test1() {
console.log(a);
let a = 100;
}
test1(); // Reference Error
let a = 10;
function test2() {
console.log(a);
}
test2(); //10
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);
This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 3 years ago.
var x = 2;
function fun() {
x = 3;
var x = 4;
document.write(x);
}
document.write(x);
fun()
document.write(x);
Can someone help me understand the flow of control. Why is the output 242 when it looks like output should be 243. All help will be greatly appreciated.
This due to hoisting. The variable x which is local inside fun is brought to top of scope and then assigned the value 3 and after that assign value 4. So line x=3; is not changing global variable but its changing local variable. The code acts like
function fun(){
var x;
x=3;
x=4;
document.write(x);
}
When you modify the x=3 it is not actually changing the global variable x but the variable declared in the function block (as var variables have function scope). As the declaration var x is hoisted to the top and then the modification to x = 3 happens
<script>
var x=2;
function fun(){
//var x; hoisted to the top;
console.log("x is hoisted here and uninitialized value will be", x)
x=3; //initialized, actually referring to the variable declared in the function scope
var x = 4; //declared but hoisted at the top
document.write(x);
}
document.write(x);
fun()
document.write(x);
</script>
To really change the variable at the global scope, use window.x to refer to it:
<script>
var x=2;
function fun(){
window.x=3; //modifying the global variable 'x`
var x = 4;
document.write(x);
}
document.write(x);
fun()
document.write(x);
</script>
This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 5 years ago.
I was running following JavaScript:
var foo = function() {
var a = 3,
b = 5;
var bar = function() {
var b = 7,
c = 11;
a += b + c;
console.debug(d);
};
bar();
console.debug(c);
var d = 10;
};
foo();
Clearly, d is not known to nested function bar and c is not known to external function foo. But in developer tools, when I load my web page I get two different logs one by one:
undefined
Uncaught ReferenceError: c is not defined
Why are the errors different for the two cases? In my opinion both should have thrown simply thrown reference error if the corresponding variable is not known to them or is out of scope.
The variable c died when function foo returned; c is local to bar because a var qualifier was used.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript Pass Variables Through Reference
How can you do something like this in javascript?
In PHP you can put a & in a function in front of the parameter to write directly back to the variable.. How can you do something like this in javascript?
In JavaScript, values are passed to functions by value. Objects, however, are passed by reference.
Passing values:
function myfunction(x)
{
// x is equal to 4
x = 5;
// x is now equal to 5
}
var x = 4;
alert(x); // x is equal to 4
myfunction(x);
alert(x); // x is still equal to 4
Passing objects:
function myobject()
{
this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6
Source: http://snook.ca/archives/javascript/javascript_pass
If you pass an object into a function it is always treated as passing a reference to it.
To have a similar effect for primitive values, you might wrap them in an object before like
var intVal = { val: 0 };
That way you can pass it to a function and still modify the actual variable.