javascript不同的OOP方法标准 [英] javascript different OOP methods standards

查看:78
本文介绍了javascript不同的OOP方法标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最长的时间内,我通过执行以下操作来制作JavaScript类

For the longest time i have been doing JavaScript classes by doing the following

function MyClass(){
    this.v1;
    this.foo = function(){
        return true;
    }
};

然后我找到了TypeScript,它看起来像将其Classs编译为

Then i found TypeScript and it looks like it compile its Classes down to

var MyClass = (function () {
    function MyClass(message) {
        this.v1 = message;
    }
    MyClass.prototype.foo = function () {
        return "Hello, " + this.v1;
    };
    return MyClass;
})();

我在网络上发现了很多其他方法是

Another Method i have found a lot on the web is

var MyClass = {
    v1:1,
    foo: function(){
       return true;
    }
};

在我看来,这很丑陋,但也许我缺少使用此方法的好处,因为它似乎是网络上大多数人在javaScript中处理对象的方式.

This looks ugly to me but maybe i'm missing something beneficial with this method, as it looks to be the way most people on the web do objects in javaScript.

我能够从我制作的函数中进行骚扰的第一种方法.

The first method i'm able to do inharitance from a function i made.

Function.prototype.extends = function(parent){
    this.prototype = new parent();
    this.prototype.constructor = this;
};

我还看到了许多其他使用JavaScript制作类的方法.我想知道我的方法是否错误,或者在进行OOP时是否有最佳实践方法.我完成的所有项目都使用第一个示例.现在,您可以在 https://github.com/Patrick-W-McMahon 上看到JavaScript编译器在做什么,我开始质疑我的方法.我想知道其他JavaScript程序员建议的最佳方法是什么,以及这些方法之间是否存在差异.我来自C ++/Java背景,因此我写JavaScript来匹配我的背景.

I have also seen many other methods on making classes in JavaScript. I would like to know if my method is wrong or if there is a best practice method in doing OOP. All of my projects I have done use the first example. You can see on https://github.com/Patrick-W-McMahon now that I see what JavaScript compilers are doing i'm starting to question my methods. I would like to know what other JavaScript programmers advise is the best method and if there is a difference between the methods. I came from a C++/Java background and as such I write my JavaScript to match my background.

推荐答案

您在此处阅读的内容称为电源构造函数,在此处了解更多信息:

What you read here is called the power constructor, more about it here:

Douglas Crockford关于继承

作为一种多范式语言,JavaScript非常适合这些不同的考虑.事实是,它会以一种可以构想的方式在您的大脑周围发霉.

As a multi-paradigm language JavaScript lends itself very well to these different considerations. The truth is, it molds around your brain the way you can conceive it.

换句话说,如果您可以以一种方式推断出结构,则可以以这种方式使用.

In other words, if you can reason about your structure in one way, than use it that way.

另一方面,如果您想完全摆脱"自己的约束,那么您应该完全放弃类的推理,并接受原型继承,而无需任何类似于类的 全部.那么鸭式打字是很自然的结果,在极端情况下,您会在各处使用闭包.

If you, on the other hand, want to be completely "free" of your own constraints, then you should abandon reasoning in classes altogether and embrace prototypal inheritance without any thing that resembles a class at all. Duck typing is then a natural consequence and in the extreme you'd use closures all over the place.

我经常这样做:

function myItemBuilder(p1)
{
  var s0, p0 = 0;

  // s0 is a shared private property (much like static in classes)
  // p0 is a shared private property but will be factorized (rendered non-shared)
  // p1 is a private instance property

  return (function (p0) { // factorize p0 if necessary

    return {
      publicProperty : 3,
      method1 : function (arg1) {
        // code here (may use publicProperty, arg1, s0, p0 (factorized) and p1)
      },
      method2 : function (arg2) {
        // code here (may use publicProperty, arg2, s0, p0 (factorized) and p1)
      }
    };
  }(p0++)); // on each invocation p0 will be different and method1/method2 will not interfere across invocations (while s0 is shared across invocations)
}

编辑:仅当您需要分解p0时才需要内部闭包(即,每次调用时p0具有单独的独立值).当然,如果不需要p0因式分解,请省略内部闭包.

The internal closure is only required if you need to factorize p0 (i.e. have separate, independent values for p0 on each invocation). Of course, if you don't need p0 factorized omit the internal closure.

上面的示例故意比说明各种有趣的情况要复杂.

The above example is intentionally more complex than required to illustrate various interesting cases.

调用类似myItemBuilder("hello")的方法会构建具有这些特定功能的新项目,但实际上并没有class构造本身.

Invoking this method like myItemBuilder("hello") builds a new item that has those specific features but there really is no class construct per se.

当您要放弃经典继承时,这是获取实例的一种特别强大的方法.例如,在C ++中,您可以从多个类中继承,这称为混合.在Java和C#中,您只有单继承性,但可以使用接口.

This is an especially powerful way of getting instances when you want to abandon classical inheritance. For example in C++ you can inherit from more than one class, which is called a mix-in. In Java and C# you only have single-inheritance but interfaces come to your help.

在上面,我在上面显示的是装配线隐喻,它可以将组件装配到实例中,结果是没有 concrete (1)类,接口和混入之间的区别.它们都是具有功能的实例,您可以通过元编程(鸭子输入,反射/检查)来反思它们.仍然存在逻辑(2)的不同之处:(1)具体实例的行为与它们的形成方式无关,但(2)从逻辑上讲,接口是契约,而实例是实施.

Here, what I've shown above, is the assembly line metaphor which can assemble components into an instance, with the result that there is no concrete (1) difference between class, interface and mix-in. They are all just instances that have features you can reflect upon through meta-programming (duck typing, reflection/inspection). There still is a logical (2) difference in that: (1) concrete instances behave the same independently of the way they come to being but (2) logically an interface is a contract while an instance is an implementation.

如果您确实了解 SOLID

If you really understand SOLID and the Assembly Line Metaphor all of this makes sense. Otherwise I beg your pardon for this long answer :)

添加:

使用这种方法,您无法检查类型,但是您不必这样做,因为鸭子输入使您无需查看类或接口协定就可以找到所需的方法.这类似于将每种方法放在单独的单方法界面中.

With this approach you can't check types, but you don't need to because duck typing allows you to find the method you need without having to look at a class or interface contract. This is similar to having each method in a separate single-method interface.

这篇关于javascript不同的OOP方法标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆