1

I have built an object in Javascript on the Google Apps Script engine and every time I run my script I get a reference error saying uName is not defined.

Here is the relivant code:

function DataSet()
{
  this.uName = "";
  this.dField = "";
  this.qUrl = "http://api.bfbcs.com/api/pc?players="+uName+"&fields="+dFeilds;
  this.data = "";

  this.dQuery = dQuery;
  this.execQuery = execQuery;

According to all sources I have found, I should not need to use the keyword var, and when I do include that, it throws other errors.

What could be going on?

Thanks

2 Answers 2

4

Well, yes, the variable uName isn't defined, in the snippet you posted. Neither's dQuery or execQuery, or dFeilds (spelling!). Are they coming from other code you haven't shown us?

There's a property this.uName, but object properties are a completely different thing to variables in JavaScript. Unlike Java, they don't share a namespace.

Also, you need to URL-encode parameters. eg.:

this.qUrl = "http://api.bfbcs.com/api/pc?players="+encodeURIComponent(this.uName)+"&fields="+encodeURIComponent(this.dField);
Sign up to request clarification or add additional context in comments.

5 Comments

@OP: Following on from Bob's points, this in Javascript is very different from this in languages like C, C#, or Java. Don't let the similar name and (in some ways) similar functionality deceive you. Amongst other things, you can never leave off this as you can in those others, it's never assumed in Javascript.
@bobince, Thanks, I made the appropriate changes and made the URL changes. everything is working correctly now.
Added a missing this. in front of dField in the URL.
Aside: which is better etiquette? Correcting a minor error in an answer or leaving a comment pointing the error out and leave the editing to the answer author?
I omitted the this because it wasn't clear to me that dFeilds in the plural was supposed to be the same as dFeild. You're probably right though! [meta: And for minor errors an edit is fine... I think I'd probably tend to edit more on an already-accepted answer and comment more when there's no accepted answer, but I can't wholly explain that.]
1

I am not sure what you are trying to do but I dont see your function receiving those parameters:

function DataSet(uName,dFeilds,dQuery,execQuery)
{
  this.uName = "";
  this.dFeild = "";
  this.qUrl = "http://api.bfbcs.com/api/pc?players="+uName+"&fields="+dFeilds;
  this.data = "";

  this.dQuery = dQuery;
  this.execQuery = execQuery;

2 Comments

In the examples I looked at on the web at the W3C they did not pass in parameters for methods like teh ones at the bottom. They did however for properties, but I saw elsewhere thatyou could forgo that. It seems a little redundant to have to specify the variable for a class then specify the property and assign the variable to that property.... but then it is OK to update that property directly through class methods, leaving the variable to only have the purpose of creating a defined memory location. Unless I could do something like this: this.uName = var uName;
Declaring var isn't anything to do with memory locations, it is to do with specifying the block in which a variable will be local. Arguments are automatically local variables so you don't need to var them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.