INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Returning a Private Var

Returning a Private Var

(OP)
Hey guys I am new to debugging JavaScript in firebug and am trying to do something pretty basic and getting frustrated.

here is my code that I am loading in firebug
//Code Starts
function Person(first,last,age){
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
this.getBalance = function(){
return bankBalance;
};
}
var Patrick = new Person('Patrick','Smith',30);
var myBalance = Patrick.getBalance();
//Code Ends
In Firebug I am typing
console.log(myBalance);
It returns undefined, why is that?

RE: Returning a Private Var

Hi

I get 7500 with FireBug 2.0.12 running in FireFox 40.0.3.

Feherke.
feherke.ga

RE: Returning a Private Var

(OP)
Feherke, did you save it as a html file or js file?

I tried putting script tags around the js and saved it as html and retried it in firebug and it worked.
Do most of you experience firebug users add script tags around your JS and save it as html before testing?

RE: Returning a Private Var

Your code works for me just fine. It returns 7500, in the console, and if I alert the value.

Is that exactly the way your code is when you test?

Are you sure its this code, and not something else that is returning the undefined value?

What browser are you testing this in?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Returning a Private Var

(OP)
The code works fine for me when I put it in script tags and save it as html, but when I try it as a .js file it returns undefined. Are you saving it as .html with script tags or running it as is?

that exact code saved as a .js file returns undefined in the latest version of firefox and firebug.

RE: Returning a Private Var

And how are you running the Js file? Js files are not something you can execute directly. They need to be called either by an HTML file, or another Js file that is itself called by an HTML file.

i.e Js files are not stand alone.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Returning a Private Var

(OP)
Ok thanks for your help I get it now. For some reason I believed firebug was able to run standalone .js

Thanks for clearing it up for me.

Also just to answer your question vacunita, I was referencing a js file on my desktop with the firefox browser.

RE: Returning a Private Var

No, it does not work like that. What you were doing was just opening a text file in Firefox. So you were just looking at text.

No code is run that way.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Returning a Private Var

Hi

Quote (SharePointPunk)

For some reason I believed firebug was able to run standalone .js
(...)
I was referencing a js file on my desktop with the firefox browser.

For standalone JavaScript try Node.js :

CODE --> line

master # cat SharePointPunk.js 
//Code Starts
function Person(first,last,age){
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
this.getBalance = function(){
return bankBalance;
};
}
var Patrick = new Person('Patrick','Smith',30);
var myBalance = Patrick.getBalance();
//Code Ends

console.log(myBalance);

master # nodejs SharePointPunk.js 
7500 
Above example run in Linux, but should run identically on Windows too. Note that browser is not involved in any way, so DOM and other browser-specific additions are not available.

Feherke.
feherke.ga

RE: Returning a Private Var

Or you can run this JS on Windows (with Windows Script Host),
only redefine console.log to WScript.Echo - fo example:

CODE

// Console for WSH
var console = {
  log:  function (line) {
    WScript.Echo(line)
  }
}


//Code Starts
function Person(first,last,age){
  this.firstname = first;
  this.lastname = last;
  this.age = age;
  var bankBalance = 7500;
  this.getBalance = function(){
    return bankBalance;
  };
}

var Patrick = new Person('Patrick','Smith', 30);
var myBalance = Patrick.getBalance();
console.log(myBalance);
//Code Ends 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close