Posts: 3,110
Location: Toronto, Ontario
|
Variables defined in the global scope are global and can be used anywhere. If you leave off the 'var' keyword when initializing a variable within a different scope (a function), then it will be considered global.
As for your test, it should work just fine. I just tried it myself becuase I thought it odd, but sure enough it worked as expected
As for OOP, anything can be an object. If you wanted to oop'ize this example, it be be:
Code:
var example = {
int_variable: '',
print_text: function()
{
alert(example.int_variable);
}
}
.....
example.int_variable = 'Tihs is a test';
example.print_text()
This is a single instance of the object. The instance is still a variable, so scope rules still apply of course. You can make multiple instancable objects:
Code:
function example()
{
// Init
this.int_variable = '';
this.print_text = function()
{
alert(this.int_variable);
}
}
....
ex = new example();
ex.int_variable = 'This is a test';
ex.print_text();
Everything is an object, so you can extend it all as you wish  You can methods to single instances of an object, or add methods to all objects (called a prototype). Depends what you need to do 
|