简体   繁体   English

Javascript对象属性的参考错误

[英]Reference Error for property of Javascript Object

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. 我已经在Google Apps脚本引擎上使用Javascript构建了一个对象,每次运行脚本时,都会收到一个参考错误,提示未定义uName。

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. 根据我发现的所有消息来源,我不需要使用关键字var,并且在包含该关键字时会引发其他错误。

What could be going on? 可能会发生什么?

Thanks 谢谢

Well, yes, the variable uName isn't defined, in the snippet you posted. 嗯,是的,变量uName 没有定义,在您发布的片断。 Neither's dQuery or execQuery , or dFeilds (spelling!). 既不是dQuery还是execQuery ,也不是dFeilds (拼写!)。 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. 有一个属性 this.uName ,但对象属性与JavaScript中的变量完全不同。 Unlike Java, they don't share a namespace. 与Java不同,它们不共享名称空间。

Also, you need to URL-encode parameters. 另外,您需要对参数进行URL编码。 eg.: 例如。:

this.qUrl = "http://api.bfbcs.com/api/pc?players="+encodeURIComponent(this.uName)+"&fields="+encodeURIComponent(this.dField);

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM