ES6:构造函数 getter 和 setter

发布于 2025-01-12 18:06:43 字数 459 浏览 0 评论 0原文

为什么我不能在构造函数中以这种方式设置 getter 和 setter?

function zConstructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    
    set fullname(text) {
        const parts = text.split(' ');
        this.firstName = parts[0];
        this.lastName = parts[1];
    }

    get fullname() {
        return this.firstName + ' ' + this.lastName;
    }
}

这种 getter 和 setter 方式仅适用于类和工厂函数。原因是什么?

谢谢!

Why can't I set getters and setters in this way inside of the Constructor function?

function zConstructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    
    set fullname(text) {
        const parts = text.split(' ');
        this.firstName = parts[0];
        this.lastName = parts[1];
    }

    get fullname() {
        return this.firstName + ' ' + this.lastName;
    }
}

This getters and setters way works only in classes and Factory functions. What is the reason?

Thanks!

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

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

发布评论

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

评论(2

意犹 2025-01-19 18:06:43

可以做到这一点,但是构造函数只是一个函数,普通代码块的语法不包括setter和getter函数的创建;就解析器而言,甚至无法理解代码的含义。

您可以使用 Object.defineProperties() 添加属性。或者,更好的方法是直接在原型上创建它们(再次使用 Object.defineProperties()),或者使用 class 声明:

function zConstructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    
    Object.defineProperties(this, {
        fullname: {
            set: function(text) {
                const parts = text.split(' ');
                this.firstName = parts[0];
                this.lastName = parts[1];
            },
            get: function() {
                return this.firstName + ' ' + this.lastName;
            }
        }
    });
}

You can do it, but a constructor function is just a function, and the syntax of ordinary code blocks does not include the creation of setter and getter functions; there's no way to even make sense of what your code is supposed to mean, as far as the parser is concerned.

What you can do is use Object.defineProperties() to add the properties. Or, probably better, is to create them on the prototype either directly (again, with Object.defineProperties()), or by using a class declaration:

function zConstructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    
    Object.defineProperties(this, {
        fullname: {
            set: function(text) {
                const parts = text.split(' ');
                this.firstName = parts[0];
                this.lastName = parts[1];
            },
            get: function() {
                return this.firstName + ' ' + this.lastName;
            }
        }
    });
}
萧瑟寒风 2025-01-19 18:06:43

执行此操作的正确方法是:

function zConstructor(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}
zConstructor.prototype = {
  set fullname(text) {
    const parts = text.split(" ");
    this.firstName = parts[0];
    this.lastName = parts[1];
  },

  get fullname() {
    return this.firstName + " " + this.lastName;
  },
};

您的代码不起作用,因为 setter 和 getter 仅对对象有意义。

A proper way of doing this is:

function zConstructor(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}
zConstructor.prototype = {
  set fullname(text) {
    const parts = text.split(" ");
    this.firstName = parts[0];
    this.lastName = parts[1];
  },

  get fullname() {
    return this.firstName + " " + this.lastName;
  },
};

Your code does not work because setters and getters are meaningful for Objects only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文