I can't get my javascript function to work.
if (!cHp) { cHp = 200 };
Works like a charm, but
function checkNull(x, y) {
if (!x) { x = y; }
}
Doesn't work at all.
What am I doing wrong?
I can't get my javascript function to work.
if (!cHp) { cHp = 200 };
Works like a charm, but
function checkNull(x, y) {
if (!x) { x = y; }
}
Doesn't work at all.
What am I doing wrong?
It does work, but it doesn't do you any good because JavaScript is purely call-by-value. That means that in your function, the parameters "x" and "y" contain copies of the values present in the calling environment.
Thus,
checkNull(b, 2 + 17);
causes copies of the value of the variable "b" and the number 19 to be passed to the function. The function can do whatever it wants to "x" and "y", but that'll make no difference to the variable "b" in the calling context.
edit — OK to expand on this topic and cut off the over-long comment thread, let's talk about what "call-by-value" (and, for contrast, "call-by-reference") means. In a call-by-value parameter passing scheme, the values of actual parameters in the calling environment are copied into the parameter variables of a called function. In a call-by-reference scheme, a reference to some sort of variable in the calling environment is passed to the called function. How are those two different?
In a call-by-value scheme, this is what happens:
var a = 1;
function change(b) {
b = 2;
}
change(a);
alert(a); // still 1
In a call-by-reference scheme, however, this is true:
// FANTASY CODE - NOT REAL JAVASCRIPT
var a = 1;
function change(b) {
b = 2;
}
change(a);
alert(a); // it is 2 in this fantasy call-by-reference code
Note that one implication of a call-by-reference scheme is that the actual parameter in the calling environment must be a variable or an object property or something; that is, it must be what's called an "l-value" in programming language terms. Another way to say it is that it must be something that can appear on the left side of an assignment expression.
Things get a little confusing because of the way variables in JavaScript take on objects as their "value". When you assign an object to a variable:
var myObj = {};
you're really assigning a reference to the object. That's completely transparent in JavaScript and you don't really need to think about it. Juggling object values is as easy as primitive values in JavaScript. However, what it means is that when an object value is passed to a function, the fact that the actual value of the variable is a reference to the object means that the call-by-value semantics of JavaScript seem similar to a call-by-reference scheme:
var a = { property: 1 };
function change(b) {
b.property = 2;
}
change(a);
alert(a.property); // it's 2!! OMG call-by-reference! (not)
Yes, it's possible for a function to modify the value of a property on an object passed as a parameter. Is that call-by-reference? No, it's not, at least not if you care about what the term "call-by-reference" means. Even though that function can change the value of the "property" property, it still cannot change the value of "a" itself! That's what call-by-reference means.
There are other parameter-passing schemes rarely seen nowadays. One that's kind-of fascinating is "call-by-name" from ALGOL 60. What that scheme did was to treat the actual parameter sort-of like an implicit function, such that in the called function a reference to the parameter would re-evaluate the parameter expression. There's also "call-by-value/result", which means that the actual parameter is passed by value, but when the function returns the then-current value of the parameter in the function is implicitly copied back to the source variable in the calling environment. (That scheme also requires the actual parameter to be an l-value, like call-by-reference.)
What you need to do is have the function return a value and assign the return to a variable. Try something like this:
var checkNull = function (x, y) {
if (!x) {
return y;
}
return x;
},
cHp,
testVar;
cHp = checkNull(testVar, 200); // will return 200 since testVar is undefined
testVar = 100;
cHp = checkNull(testVar, 200); // will return 100 since testVar is defined