我只是尝试在对象内部运行一个函数,但它返回未定义的输出。

huangapple go评论77阅读模式
英文:

I am just trying to run a function inside an object but it returns undefined output

问题

const rajat = {
  FirstName: "Rajat",
  middleName: "Kumar",
  lastName: "sahoo",
  birthYear: 1998,
  location: "Bhubaneswar",
  job: "Programmer",
  friends: ["Michel", "Jack", "James", "Juile"],

  calcAge: function () {
    this.Age = 2023 - this.birthYear;  // `这里如果我创建年龄对象,它会返回未定义的输出`
    return this.Age;                   // `但当我正常调用函数时,它正常工作`
  },
};

console.log(rajat.Age);

我只翻译了代码部分,不包括问题和注释。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const rajat = {
  FirstName: &quot;Rajat&quot;,
  middleName: &quot;Kumar&quot;,
  lastName: &quot;sahoo&quot;,
  birthYear: 1998,
  location: &quot;Bhubaneswar&quot;,
  job: &quot;Programmer&quot;,
  friends: [&quot;Michel&quot;, &quot;Jack&quot;, &quot;James&quot;, &quot;Juile&quot;],

  calcAge: function () {
    this.Age = 2023 - this.birthYear;  // `here if i creat the age object it give undefine output`
    return this.Age;                   // `But when i normally call the function it works properly`
  },
};

console.log(rajat.Age);

<!-- end snippet -->

I am just trying to calculate age using this method by creating an object but whenever i am trying to create the age object the output that I get is undefined why???

答案1

得分: 1

Consider making the Age a getter property:

const rajat = {
  FirstName: "Rajat",
  middleName: "Kumar",
  lastName: "sahoo",
  birthYear: 1998,
  location: "Bhubaneswar",
  job: "Programmer",
  friends: ["Michel", "Jack", "James", "Juile"],
  get Age() {
    return 2023 - this.birthYear;
  }
};

console.log(rajat.Age);
英文:

Consider making the Age a getter property:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const rajat = {
  FirstName: &quot;Rajat&quot;,
  middleName: &quot;Kumar&quot;,
  lastName: &quot;sahoo&quot;,
  birthYear: 1998,
  location: &quot;Bhubaneswar&quot;,
  job: &quot;Programmer&quot;,
  friends: [&quot;Michel&quot;, &quot;Jack&quot;, &quot;James&quot;, &quot;Juile&quot;],
  get Age() {
    return 2023 - this.birthYear;
  }
};

console.log(rajat.Age);

<!-- end snippet -->

答案2

得分: 0

你在打印之前没有调用rajat.Age()。调用一个函数就像是在说你打算使用那个函数。

英文:

You didn't call rajat.Age() before printing it. Calling a function is like saying that you are going to use that function.

答案3

得分: -1

问题在于您试图直接访问 rajat.Age 属性,而没有调用 calcAge() 方法来计算并设置年龄。在JavaScript中,属性 FirstNamemiddleName 等是作为 rajat 对象的一部分定义的,但是除非您显式调用 calcAge() 方法,否则 Age 属性不会被定义。

要获得确切的年龄,您必须首先调用 calcAge() 方法,该方法将设置 Age 属性,然后您可以访问它。以下是您可以这样做的方法:

现在通过调用 calage() 方法正确设置了 age 属性,当您访问 rajat.age 时,您将获得正确的年龄,这种情况下为 25

英文:

Hii @RajaT kumar,
The problem with your code is that you are trying to access the rajat.Age property directly without calling the calcAge() method to calculate and set the age. In JavaScript, the properties FirstName, middleName, etc. are defined as part of the rajat object, but the Age property is not defined unless you explicitly call the calcAge() method.

To get the exact age, you must first call the calcAge() method, which will set the Age property, and then you can access it. Here's how you can do it:

Enter the code Image

Now the age property is set correctly by calling the calage() method, and when you access rajat.age, you get the correct age, which in this case is 25.

huangapple
  • 本文由 发表于 2023年7月20日 09:26:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定