var foo = function(){};
foo.prototype.value = 5;
function bar(obj)
{
obj.value = 6; // I'm changing it to 6!
}
bar(foo);
alert(foo.value); // it's now 6!
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 24
Now, what happens if you try to pass in a function?
var foo = function(){};
foo.prototype.value = 5;
foo.prototype.addValue = function(){ foo.value = 6; }
function bar(func)
{
func(); // I'm running the function!
}
bar(foo.addValue); // pass in the function
alert(foo.value); // it's now 6!
There are a couple of different things going on in this example. First, when you pass in the
function, don??™t include the round brackets () because you can pass the function in without
actually executing the code contained within it. While not evident, you are actually making a
copy of the function (just like the variable) and passing that in. Later on, I??™ll cover some things
to look out for with context and object referencing.
Had you included the brackets, the function would have run immediately, and the return
value of the function would have been passed through. Here??™s a quick demonstration:
function foo()
{
return 6; // return a value
}
function bar(val)
{
alert(val);
}
bar(foo()); // shows 6
JavaScript and the DOM
JavaScript is the magician that brings the HTML and CSS to life! JavaScript, the language, is
pretty straightforward and I??™m assuming that you??™ve had some experience using JavaScript or
at least understand the basics of the JavaScript syntax.
Pages:
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70