如何更改javascript原型对象的属性

时间:2022-02-25 19:00:13

html base

<html>
  <head>
  </head>
  <body>
    <input type="text" class="abc"/>
  </body>
</html>

So I have my prototype object

所以我有我的原型对象

function AnArray(){
    this.anArray=[];
}

AnArray.prototype.getAnArray=function(val){
    return this.anArray[val];
}

AnArray.prototype.setData=function(index,val){
    this.anArray[index].data=val;
}

var objAnArray=new AnArray();

the object ends up looking like this

对象最终看起来像这样

id: 1, pid: "questions-worth-asking", num: 1, data: null

and I'm trying to change an attribute in it like so

而我正试图改变它的属性

objAnArray.setData(0,$(".abc").eq(0).val());

When I've rune console.log messages using getAnArray() before and after the above line, it returns the same value as it has not been changed.

当我在上面的行之前和之后使用getAnArray()编辑console.log消息时,它返回相同的值,因为它没有被更改。

My question is how do you change attributes of a prototype object?

我的问题是你如何更改原型对象的属性?

edit: This link led me down the right path http://www.gpickin.com/index.cfm/blog/websql-when-is-a-javascript-object-not-a-javascript-object

编辑:这个链接让我走上了正确的道路http://www.gpickin.com/index.cfm/blog/websql-when-is-a-javascript-object-not-a-javascript-object

2 个解决方案

#1


You're missing a lot of information from your post that makes it difficult to debug.

您的帖子中缺少大量信息,这使得调试变得困难。

From my understanding the problem is that:

根据我的理解,问题在于:

  1. You want your jQuery object's value property to be stored in an array that you wrapped in an object.
  2. 您希望将jQuery对象的value属性存储在包装在对象中的数组中。

  3. You want to store this property with the setData function you wrote.
  4. 您希望使用您编写的setData函数存储此属性。

  5. You want to retrieve that object by using the getAnArray function you wrote.
  6. 您希望使用您编写的getAnArray函数来检索该对象。

I don't think this is an issue with prototypes, but with the lack of information given to us, it could be any number of things. I wouldn't be surprised if you came back and said "Oh, it was something else completely."

我不认为这是原型问题,但由于缺乏给我们的信息,它可能是任何数量的东西。如果你回来说“噢,这完全是另一回事”,我不会感到惊讶。

I've made a fiddle that successfully sets and gets data from the anArray objects that you can use as an example.

我已经成功地设置并从anArray对象中获取数据,您可以将其用作示例。

Here are some problems you want to look at that will help you debug:

以下是您希望查看的一些问题,可帮助您进行调试:

  1. You don't set the anArray[index] object in this snippet. So if we are to take this at face value, the setData function should throw a ReferenceError.
  2. 您没有在此代码段中设置anArray [index]对象。因此,如果我们要以面值取值,setData函数应该抛出一个ReferenceError。

  3. You haven't told us if you're calling the setData function inside an event or right when the page loads. If it's the latter, then according to the html you posted at the top, you won't have any data in the input field yet. You need to call the setData function only when there's data in the field.
  4. 你没有告诉我们你是在一个事件中调用setData函数还是在页面加载时调用它。如果是后者,那么根据你在顶部发布的html,你还没有输入字段中的任何数据。只有当字段中有数据时才需要调用setData函数。

  5. You might be calling the jQuery object outside of the $(document).ready(function(){ ... }) call so the value you're obtaining with $(".abc") call is undefined.
  6. 您可能在$(document).ready(function(){...})调用之外调用jQuery对象,因此使用$(“。abc”)调用获取的值是未定义的。

Give those a try and hopefully those work.

尝试一下,希望那些工作。

To make your questions easier to debug going forward:

为了使您的问题更容易进行调试:

  1. Write all your experimental code in an isolated environment so that all the confidential content content doesn't have to be removed before posting.
  2. 将所有实验代码写在隔离的环境中,以便在发布之前不必删除所有机密内容内容。

  3. Run your code and make sure it runs as expected.
  4. 运行您的代码并确保它按预期运行。

  5. Show us all of that code so that we know where all the data comes from and how each element interacts with the other elements. For example, we currently don't know how the anArray array is populated so I've had to make assumptions. We also don't know how id, pid, or "questions-worth-asking" comes from so there might be side effects from how those are loaded in.
  6. 向我们展示所有代码,以便我们知道所有数据的来源以及每个元素如何与其他元素交互。例如,我们目前不知道如何填充anArray数组,所以我不得不做出假设。我们也不知道id,pid或“问题值得问题”是如何产生的,因此可能会产生副作用。

  7. Write your code using some sort of style guide to make it easier to read. This will also help improve debug time for you and will help prevent errors from silly mistakes that you might make.
  8. 使用某种样式指南编写代码,以便于阅读。这也有助于缩短您的调试时间,并有助于防止您可能犯的错误。

Edit: I know you're calling console.log before and after the setData method. Consider putting console.log inside the setData method as well.

编辑:我知道你在setData方法之前和之后调用console.log。考虑将console.log放在setData方法中。

AnArray.prototype.setData = function (index, val) {
    console.log("Entering setData with: ", index, val, this.anArray[index]);
    this.anArray[index].data = val;
    console.log("Exiting setData with: ", this.anArray[index]);
};

#2


It seems to me that the problem isn't in your javascript. You're saying that you ran a console.log before and after your setData call on objAnArray. Perhaps it has something to do with your HTML input element not being updated, or the data not coming through in time.

在我看来,问题不在你的JavaScript中。你说你在objAnArray上调用setData之前和之后运行了一个console.log。也许它与您的HTML输入元素未更新或数据未及时通过有关。

Like Keane said, we need more info about your set up and logic flow.

像Keane所说,我们需要有关您的设置和逻辑流程的更多信息。

#1


You're missing a lot of information from your post that makes it difficult to debug.

您的帖子中缺少大量信息,这使得调试变得困难。

From my understanding the problem is that:

根据我的理解,问题在于:

  1. You want your jQuery object's value property to be stored in an array that you wrapped in an object.
  2. 您希望将jQuery对象的value属性存储在包装在对象中的数组中。

  3. You want to store this property with the setData function you wrote.
  4. 您希望使用您编写的setData函数存储此属性。

  5. You want to retrieve that object by using the getAnArray function you wrote.
  6. 您希望使用您编写的getAnArray函数来检索该对象。

I don't think this is an issue with prototypes, but with the lack of information given to us, it could be any number of things. I wouldn't be surprised if you came back and said "Oh, it was something else completely."

我不认为这是原型问题,但由于缺乏给我们的信息,它可能是任何数量的东西。如果你回来说“噢,这完全是另一回事”,我不会感到惊讶。

I've made a fiddle that successfully sets and gets data from the anArray objects that you can use as an example.

我已经成功地设置并从anArray对象中获取数据,您可以将其用作示例。

Here are some problems you want to look at that will help you debug:

以下是您希望查看的一些问题,可帮助您进行调试:

  1. You don't set the anArray[index] object in this snippet. So if we are to take this at face value, the setData function should throw a ReferenceError.
  2. 您没有在此代码段中设置anArray [index]对象。因此,如果我们要以面值取值,setData函数应该抛出一个ReferenceError。

  3. You haven't told us if you're calling the setData function inside an event or right when the page loads. If it's the latter, then according to the html you posted at the top, you won't have any data in the input field yet. You need to call the setData function only when there's data in the field.
  4. 你没有告诉我们你是在一个事件中调用setData函数还是在页面加载时调用它。如果是后者,那么根据你在顶部发布的html,你还没有输入字段中的任何数据。只有当字段中有数据时才需要调用setData函数。

  5. You might be calling the jQuery object outside of the $(document).ready(function(){ ... }) call so the value you're obtaining with $(".abc") call is undefined.
  6. 您可能在$(document).ready(function(){...})调用之外调用jQuery对象,因此使用$(“。abc”)调用获取的值是未定义的。

Give those a try and hopefully those work.

尝试一下,希望那些工作。

To make your questions easier to debug going forward:

为了使您的问题更容易进行调试:

  1. Write all your experimental code in an isolated environment so that all the confidential content content doesn't have to be removed before posting.
  2. 将所有实验代码写在隔离的环境中,以便在发布之前不必删除所有机密内容内容。

  3. Run your code and make sure it runs as expected.
  4. 运行您的代码并确保它按预期运行。

  5. Show us all of that code so that we know where all the data comes from and how each element interacts with the other elements. For example, we currently don't know how the anArray array is populated so I've had to make assumptions. We also don't know how id, pid, or "questions-worth-asking" comes from so there might be side effects from how those are loaded in.
  6. 向我们展示所有代码,以便我们知道所有数据的来源以及每个元素如何与其他元素交互。例如,我们目前不知道如何填充anArray数组,所以我不得不做出假设。我们也不知道id,pid或“问题值得问题”是如何产生的,因此可能会产生副作用。

  7. Write your code using some sort of style guide to make it easier to read. This will also help improve debug time for you and will help prevent errors from silly mistakes that you might make.
  8. 使用某种样式指南编写代码,以便于阅读。这也有助于缩短您的调试时间,并有助于防止您可能犯的错误。

Edit: I know you're calling console.log before and after the setData method. Consider putting console.log inside the setData method as well.

编辑:我知道你在setData方法之前和之后调用console.log。考虑将console.log放在setData方法中。

AnArray.prototype.setData = function (index, val) {
    console.log("Entering setData with: ", index, val, this.anArray[index]);
    this.anArray[index].data = val;
    console.log("Exiting setData with: ", this.anArray[index]);
};

#2


It seems to me that the problem isn't in your javascript. You're saying that you ran a console.log before and after your setData call on objAnArray. Perhaps it has something to do with your HTML input element not being updated, or the data not coming through in time.

在我看来,问题不在你的JavaScript中。你说你在objAnArray上调用setData之前和之后运行了一个console.log。也许它与您的HTML输入元素未更新或数据未及时通过有关。

Like Keane said, we need more info about your set up and logic flow.

像Keane所说,我们需要有关您的设置和逻辑流程的更多信息。

相关文章