Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 05-24-2006, 01:30 AM Javascript & OOP
Webmaster Talker

Posts: 626
Trades: 0
I am trying to setup a program on my website where I can include the file into any document. In this file I have a bunch of variables which I need to have accessible to both the file I am including and the file that is *holding* the include.

For example:

index.html
Code:
<script type="text/javascript" language="javascript" src="fileincluded.js"></script>

int_variable = 'This is a test';
fileincluded.js:
Code:
var int_variable = '';

function print_text() {
    alert(int_variable);
}
If print_text() was to be executed, it should alert 'This is a test';.

My thought process is... The file is included which makes all the functions in side fileincluded.js accessible to index.html

Then, the variable is set to 'This is a test'... Because this happens after the file is included that should be the current value of the variable.

---

My problem is, I tried this process and it didn't work. What is going wrong?
jim.thornton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-24-2006, 03:26 AM Re: Javascript & OOP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
Functions can't use outside varibles unless you pass it directly in it by declaring the var to be global inside the function, or just pass it as a function var which would make more since in your example.

Code:
<script type="text/javascript" language="javascript" src="fileincluded.js"></script>

<script type="text/javascript" language="javascript"> int_variable = 'This is a test'; // Define var print_text(int_variable); // Call function while passing var // Define function function print_text(text) { alert(text); } </script>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-24-2006, 11:37 AM Re: Javascript & OOP
Webmaster Talker

Posts: 626
Trades: 0
Okay, I knew that was alway an option but I was just seeing if there was a way to set it up like a class in php where all the variables are accessible like $clsname->variable.

Guess not.

Thanks.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Old 05-24-2006, 06:18 PM Re: Javascript & OOP
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 05-24-2006, 06:41 PM Re: Javascript & OOP
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
I was wondering how globals worked in JS
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-25-2006, 02:33 PM Re: Javascript & OOP
Webmaster Talker

Posts: 626
Trades: 0
Thanks Chroder,

I'm actually surprised that worked without a problem... I just did it from scratch while I was typing the message. My situation is much bigger so I thought I would simplify it to make it easier.

Thanks though, for showing my how to convert to OOP.
jim.thornton is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript & OOP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.44382 seconds with 12 queries