这是做JS OOP的好方法吗?(Is this a good way to do JS OOP?)

编程入门 行业动态 更新时间:2024-10-27 14:18:42
这是做JS OOP的好方法吗?(Is this a good way to do JS OOP?)

我想问一下我的以下OOP风格的优点。 我按照以下方式编写我的JS类。

var MyClass = function() { // private vars var self = this, _foo = 1, _bar = "test"; // public vars this.cool = true; // private methods var initialize = function(a, b) { // initialize everything }; var doSomething = function() { var test = 34; _foo = cool; }; // public methods this.startRequest = function() { }; // call the constructor initialize.apply(this, arguments); }; var instance_1 = new MyClass(); var instance_2 = new MyClass("just", "testing");

这是一个好方法吗? 有什么缺点吗? 我不使用继承,但它会以这种方式实现继承吗?

提前致谢。

I wanted to ask about the pros cons of my the following OOP style. I write my JS classes in the following manner.

var MyClass = function() { // private vars var self = this, _foo = 1, _bar = "test"; // public vars this.cool = true; // private methods var initialize = function(a, b) { // initialize everything }; var doSomething = function() { var test = 34; _foo = cool; }; // public methods this.startRequest = function() { }; // call the constructor initialize.apply(this, arguments); }; var instance_1 = new MyClass(); var instance_2 = new MyClass("just", "testing");

Is this a good approach? Is there any drawback? I don't use inheritance, but would it work this way to achieve inheritance?

Thanks in advance.

最满意答案

我认为这是一个非常好的方法。 不要为'不传承'问题感到羞耻。 大多数面向对象不是关于继承。 最重要的方面是封装和多态,你已经得到了它们。

可以说(嗯,我通常认为)只有静态语言需要继承,你必须以某种方式告诉编译器这两种类型(类)是相关的,它们有共同的东西(共同的祖先),所以它可以允许多态。 使用动态语言OTOH,编译器不会在意,运行时环境将找到没有任何继承的共同点。

另一点:如果你在某些地方需要继承(在某些情况下它很好 ,例如GUI),通常你会发现你可以很容易地在你的'简单'对象/类和其他更复杂的重。 IOW:不要试图找到满足您所有需求并将其用于一切的框架; 而是使用你在任何时刻都更加适应的方法,只要它能帮助解决特定的问题。

I think it's a very good approach. Don't be ashamed of the 'no inheritance' issue. Most OOP isn't about inheritance. The most important aspects are the encapsulation and polymorphism, and you've got them.

It can be argued (well, i usually argue) that inheritance is only needed for static languages, where you have to somehow tell the compiler that these two types (classes) are related, that they have something in common (the common ancestor) so that it can allow polymorphism. With dynamic languages, OTOH, the compiler won't care, and the runtime environment will find the commonalities without any inheritance.

Another point: if you need some inheritance in some places (and it's great in some cases, like GUIs, for example), often you'll find that you can easily interoperate between your 'simple' objects/classes, and other more complex and heavier. IOW: don't try to find a framework that fills all your needs and use it for everything; instead use the one that you're more comfortable with at each moment, as long as it helps with the specific problem.

更多推荐

本文发布于:2023-07-26 08:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1272924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是   方法   OOP   JS   good

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!