Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a Private Var

Status
Not open for further replies.

SharePointPunk

Programmer
Sep 15, 2015
6
0
0
US
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?
 
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?
 
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
 
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.
 
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
 
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.
 
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
 
Hi

SharePointPunk said:
For some reason I believed firebug was able to run standalone .js
[gray](...)[/gray]
I was referencing a js file on my desktop with the firefox browser.

For standalone JavaScript try Node.js :
Code:
[blue]master #[/blue] 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);

[blue]master #[/blue] 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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top