Javascript:是否有关键字用于引用对象中的当前节点?

发布于 2024-10-17 06:45:49 字数 669 浏览 0 评论 0原文

考虑下面的代码:

function Animal(){
  this.type = "dog";
  this.color = {
                 stomach: "white",
                 paws: "brown",
                 face: function(){
                   if(this.color.stomach === "white"){
                      return "red";
                   }else{
                      return "blue";
                   }
                 }
}

这只颜色奇怪的狗的面部颜色取决于他胃的颜色。我想知道是否有一种语法上更简单的方法来编写“this.color.stomach”部分。即,“this”指的是主要 Animal 对象。是否有类似的关键字引用调用该关键字的父对象?例如,由于我已经在 Animal.color 中,因此不必重复该部分来获取其胃颜色(Animal.color.stomach),有没有一种方法可以直接引用颜色属性,以便它是就像“parent.stomach”,其中“parent”指的是它在其中被调用的任何属性 - 在本例中是 Animal.color?

Consider the following code:

function Animal(){
  this.type = "dog";
  this.color = {
                 stomach: "white",
                 paws: "brown",
                 face: function(){
                   if(this.color.stomach === "white"){
                      return "red";
                   }else{
                      return "blue";
                   }
                 }
}

This strangely colored dog has a face color that depends on the color of his stomach. I'm wondering if there is a more syntactically simple way of writing the "this.color.stomach" part. I.e., "this" refers to the main Animal object. Is there a similar keyword that refers to the parent object in which that keyword is called? For example, since I'm already inside Animal.color, rather than having to repeat that part to get at its stomach color (Animal.color.stomach), is there a way to directly reference the color property, so that it would be like "parent.stomach" where "parent" refers to whatever property it's being called within--in this case, Animal.color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-10-24 06:45:49

您尝试运行您的代码吗?因为 this 实际上确实引用了 color 而不是 Animal 对象。

它的工作原理如下:this 指的是调用该函数的任何对象,在正常情况下,您的 face 函数将被称为 someAnimal.color。 face() - 在这种情况下,this 已经引用了 color 对象,因此 this.color 将是一个错误而 this.stomach 实际上可以工作。

Did you try running your code? Because this actually does refer to the color and not the Animal object.

This is how it works: this refers to whatever object the function was called upon, and under normal circumstances, your face function would be called as someAnimal.color.face() -- in this case, this already refers to the color object, so this.color would be an error while this.stomach would actually work.

一梦等七年七年为一梦 2024-10-24 06:45:49
function Color(data) {
    this.stomach = data.stomach;
    this.face = data.face;
}

function Animal() {
    var self = this; // self now refers to the animal
    this.type = "Dog";
    this.color = new Color({
        face: function() {
            // in the "face" function, "this" refers to the color
            if (this.stomach === "white") { // color's stomach
                return "red";
            } else if (self.type === "Dog") { // animal's type
                return "Blue";
            }
        }
   });
}
function Color(data) {
    this.stomach = data.stomach;
    this.face = data.face;
}

function Animal() {
    var self = this; // self now refers to the animal
    this.type = "Dog";
    this.color = new Color({
        face: function() {
            // in the "face" function, "this" refers to the color
            if (this.stomach === "white") { // color's stomach
                return "red";
            } else if (self.type === "Dog") { // animal's type
                return "Blue";
            }
        }
   });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文