Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当前的项目告诉我需要研究Javascript OOP,是吗?_Javascript_Jquery_Oop_Design Patterns - Fatal编程技术网

当前的项目告诉我需要研究Javascript OOP,是吗?

当前的项目告诉我需要研究Javascript OOP,是吗?,javascript,jquery,oop,design-patterns,Javascript,Jquery,Oop,Design Patterns,我正在(实际上已经完成)一个基于用户输入的数据获取报价的项目。我已经写了代码,它工作得很好。它是使用事件和函数在javascript/jQuery中编写的,更像这样(我刚才为这个问题编写的示例,而不是实际代码): 还有更多的工作部分,但我写这篇文章是为了说明它是如何组合在一起的。它看起来真的很冗长,有很多活动部件。当需要更新时,这是一种痛苦 但是我想知道我是否应该使用OOP来实现这样的功能?我真的只是想找到javascript最佳实践的根源,并找出类似的东西应该如何真正地结合起来。我觉得我写的很

我正在(实际上已经完成)一个基于用户输入的数据获取报价的项目。我已经写了代码,它工作得很好。它是使用事件和函数在javascript/jQuery中编写的,更像这样(我刚才为这个问题编写的示例,而不是实际代码):

还有更多的工作部分,但我写这篇文章是为了说明它是如何组合在一起的。它看起来真的很冗长,有很多活动部件。当需要更新时,这是一种痛苦

但是我想知道我是否应该使用OOP来实现这样的功能?我真的只是想找到javascript最佳实践的根源,并找出类似的东西应该如何真正地结合起来。我觉得我写的很多代码虽然很有效,但并不总是最好的方式,也许是因为我自学成才,没有真正深入研究

如果功能有助于理解正在发生的事情,我会将其分解为多个部分。理解每一点的工作原理对您来说并不重要:

Things I'm expecting 

    = zip code (number)
    = product (string)
    = quantity (number)
        - Need to verify number is between x and y (e.g. - 100 to 10000)
    = artwork colors (number)
        - Need to verify number is between x and y (e.g. - 0 to 6)

Things I'm calculating

    = base price (number) 
        - Depends on the product selected
        - Depends on the quantity entered (6 tiers of pricing based on quantity)
            e.g. - "Product 1" quantity of 101 to 200 = $9, quantity of 201 to 300 = $7, etc

    = screen charge (number)
        - Depends on the number of artwork colors entered
        - Depends on the quantity entered (uses same tier as when calculating base price)
            e.g. - "Product 1" quantity of 101 to 200 add 1.00 per artwork color
        - Gets rolled into the base price
            e.g. - base price = base price + (artwork colors*quantity);

    = shipping cost (hits url via ajax to fetch rate, returns number)
        - Depends on zip code entered
        - Depends on product selected (each product has different weight)

Things I need to output

    = final price
    = shipping cost
    = base price

那么我应该使用OOP吗?我应该看看?如果是这样,在高水平上,我将如何开始?我不需要完整的代码示例,只需要知道从哪里开始。我真的希望我的代码看起来不错,并且结构合理。非常感谢您提供的任何建议、资源链接等。

与重构相比,我不太担心OOP与非OOP的区别,例如,如果您有非常类似的“setBasePrice”和“setXXX”以及“setYYY”,您应该将它们转换为通用的“setAnything()”调用。OOP可能有助于完成这项任务,但对它并不重要。您可以做的是研究MVC范式——一种逻辑架构分离,旨在将代码的组件抽象为模型、视图和控制器。它在后端语言的框架(CakePHP for PHP、Rails for Ruby、Django for Python等)中最为人所知,但也绝对适用于JS

请参阅以获取一些方便的参考资料。就个人而言,这似乎是一个简单的轻量级起点(它支持jQuery(正如我看到你提到的那样))。

我的一般测试是“我应该将这段自由浮动函数放入类的一组方法中吗”(适用于任何语言,而不仅仅是JS),很简单:我是不是到处传递相同的变量

如果是这样的话,我认为将这些变量放入类的字段中,然后将这些函数放入方法中会有帮助,因为这样所有这些方法都可以引用所有这些变量,而无需“手动”传递所有内容。此外,你可以通过这种方式更好地控制事情

例如,假设有几个函数接受一个“foo”参数并对其调用“setBar”,然后在其上设置一个“dirty”标志。使用这种设置,很容易不小心忘记设置“脏”标志。但是,通过将foo设为字段并创建setFooBar方法(调用“setBar”并设置“dirty”标志),可以确保始终设置“dirty”标志


无论如何,我无法从您提供的有限代码示例中判断您的代码是否通过了测试,但希望这能给您一些思考的东西。

所有只能通过函数式脚本编写的javascript工作都可以通过oop脚本编写完成,反之亦然。
在大多数情况下,使用javascript作为OOP语言更优雅、更强大,但并不一定更高效。
当我开始将javascript OOP的基础知识结合在一起时,我对协议继承非常着迷,以至于我到处都使用对象。。。直到我意识到我把一切都复杂化了(例如:我开始编写比我最初的任务要求复杂得多的img对象,而不是普通的2行图像预加载脚本)。

从您提供的小代码来看,我真的不知道OOP是否会给您的项目带来重大改进,但我建议您在prototipal继承和maby一些设计模式上取得一个高峰(这太神奇了),但不要忘记javascript是一种全功能语言

我总是推广oop!我认为javascript作为oop语言可以很好地工作。o'reilly有一本书叫做“javascript模式”。它是由一位名叫斯托扬·斯特凡诺夫(stoyan stefanov)的雅虎大师编写的,它非常擅长解释什么时候在js中使用oop,以及如何以一种好的方式使用oop

根据这本书,我认为您可以创建一个对象,该对象具有一个构造函数,获取您的输入数据,对其进行验证,然后保存给成员。然后它会有一些方法来执行你写的计算。这些计算将在对象创建期间存储的成员的帮助下进行

下面是一个示例类:

//class named SomeClass
//constructor taking two arguments named init1 and init2
function SomeClass(init1, init2) {
    //members, use of keyword "var" makes them private
    //logic makes the members have validated values, if input is not satisfying
    var m_init1 = init1 > 5 ? init1 : 5;
    var m_init2 = init2;
    //so that was the constructor


    //well, this is still the constructor, but
    //conceptually, it's not anymore.

    //here are some public things
    //they are method calls, and they represent functions
    //that are private in here, but are made public through
    //the use of this.bla = Bla;
    this.publicMethod = PublicMethod;
    this.otherPublicMethod = OtherPublicMethod;

    //private function, made into a public method above
    function PublicMethod() {
        //this function/method has access to private members
        //of parent function object. yay!
        return m_init1;
    };

    //private function, made into a public method above
    function OtherPublicMethod(arg) {
        if(arg > 5) {
            m_init2 = arg;
        }
    };
}

var a = new SomeClass(2, 3);
alert(a.publicMethod()); //outputs 5

您构建上述信息的方式是构建oop项目的良好文档基础。我觉得这有点像一门课。我想说的是,您的文档化模型似乎正好适合我这里的小代码示例。

太棒了。有趣的是,让我重新审视js的原因之一是最近进入了Ruby/Rails。我很快就爱上了它(以及MVC模式),现在我觉得我的代码的其他部分需要修改。我会看看这个,谢谢!我明白你的意思,你说得对。我打破了(挫折价格和getTierPrice),因为getTierPrice是相当丰满的;它可以切换几种产品,每种产品有5个基本价格。虽然需要进行一些重构,但我认为坚持使用相同的结构是导致脚本混乱的原因。确实如此,谢谢!我正是这样做的,似乎我经常回去检查产品(例如,
$('input[name=product]').val()
)和数量、邮政编码等,然后像您提到的那样,手动传递这些值。当你说“…让foo成为一个领域”你这是什么意思?我想知道,所以我可以谷歌一些例子。谢谢你的帮忙!MachineHost指的是“成员f
//class named SomeClass
//constructor taking two arguments named init1 and init2
function SomeClass(init1, init2) {
    //members, use of keyword "var" makes them private
    //logic makes the members have validated values, if input is not satisfying
    var m_init1 = init1 > 5 ? init1 : 5;
    var m_init2 = init2;
    //so that was the constructor


    //well, this is still the constructor, but
    //conceptually, it's not anymore.

    //here are some public things
    //they are method calls, and they represent functions
    //that are private in here, but are made public through
    //the use of this.bla = Bla;
    this.publicMethod = PublicMethod;
    this.otherPublicMethod = OtherPublicMethod;

    //private function, made into a public method above
    function PublicMethod() {
        //this function/method has access to private members
        //of parent function object. yay!
        return m_init1;
    };

    //private function, made into a public method above
    function OtherPublicMethod(arg) {
        if(arg > 5) {
            m_init2 = arg;
        }
    };
}

var a = new SomeClass(2, 3);
alert(a.publicMethod()); //outputs 5