简体   繁体   English

JavaScript对象属性返回未定义

[英]JavaScript object property returns undefined

I've looked through many threads already, googled a lot and tried to fix this problem on my own, but I can't. 我已经浏览了许多线程,在Google上搜索了很多,并尝试自行解决此问题,但我做不到。 I'm trying to break down my problem. 我正试图解决我的问题。

I'm receiving a String stored in the local Storage. 我收到存储在本地存储中的字符串。

var c = localStorage.getItem('item');

Then I'm passing this value into a constructor to assign this value to an object's property. 然后,我将此值传递给构造函数,以将该值分配给对象的属性。 This is the class and the constructor. 这是类和构造函数。

class dataSet {
    constructor ( item ) {
      this.item = item;
    }

    get item() {
      return this._item;
    }

With the help of a button, I call the following function on click: 借助按钮,单击时调用以下函数:

function buildSet() {
    var currentSet = new dataSet( c );
    document.write(currentSet.item);
    document.write(c);
}

The document.write(c) is just a test to see if there is any value stored, and yes, my 'test' string is stored. document.write(c)只是一个测试,以查看是否存储了任何值,是的,存储了我的“ test”字符串。 As a result, I get: 结果,我得到:

undefinedTest undefinedTest

I'm very new to JavaScript, I know that somehow the dataSet object did not get the value and didn't assign it, but I have no idea left, how to fix it. 我对JavaScript还是很陌生,我知道以某种方式dataset对象没有获得该值并且没有分配它,但是我不知道如何解决它。 I literally read every existing thread for this topic on stackoverflow but could not come up with a solution. 我确实阅读了关于stackoverflow的该主题的每个现有线程,但无法提出解决方案。 Thank you for you time. 谢谢您的时间。

Your error is within the constructor! 您的错误在构造函数中!

You are missing a _ 您缺少_

It should be this._item = item; 应该是this._item = item; instead of this.item = item; 代替this.item = item;

Thus your getter, ie: 因此,您的getter,即:

get item() {
  return this._item;
}

returns undefined as there is no _item property... 由于没有_item属性,因此返回undefined。

As someone has commented to you before, the problem here is because you are missing a _ in the constructor. 正如之前有人对您发表评论一样,这里的问题是因为您在构造函数中缺少_。 But if this doesn't solve your problem, maybe the problem is related to the value of the item in localStorage. 但是,如果这不能解决您的问题,则可能是问题与localStorage中项目的值有关。 Here you have an example of your code working with a simple String: 这里有一个使用简单String的代码示例:

 var c = 'hello world'; class dataSet { constructor (item) { this._item = item; } get item () { return this._item; } } function buildSet() { var currentSet = new dataSet(c); alert(currentSet.item); alert(c); } 
 <!DOCTYPE html> <html> <body> <button onclick="buildSet()">click me</button> </body> </html> 

lets do some checks and see the behaviour of your code, because it works as expected, the only thing is that you are missing some checks and some characters. 让我们做一些检查,看看代码的行为,因为它按预期工作,唯一的是您缺少一些检查和一些字符。

 //make sure that C has a value, maybe it is undefined from here. var c = "fakeItem"; // also lets do a test with an undefined value. var d; class dataSet { constructor(item) { this._item = item; } get item() { //you were missing a _ here. return this._item; } } function buildSet() { var currentSet = new dataSet(c); //this works because C is defined var dSet = new dataSet(d); //this does not work because D is not defined // it works as expected! console.log(currentSet.item); document.write("currentSet.item: " + currentSet.item); document.write("\\n<br>"); document.write("c: " + c); document.write("\\n<br>"); // also works as expected, it will show undefined console.log(dSet.item); document.write("dSet.item: " + dSet.item); document.write("\\n<br>"); document.write("d: " + d); } 
 <button onclick="buildSet()">SET TEST</button> 

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

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