The current project tells me that I need to look at Javascript OOP, right?

I am working on a project (and actually finished) that selects price quotes based on user input. I already wrote the code and it works great. This is written in javascript / jQuery, using events and functions more similar to this (for example, I just wrote for this question, not the actual code):

// event - when the quantity field changes
$('input[name=quantity]').change(function(){
    // get the product
    var product = $('input[name=product]').val();
    // run function to set the base price
    setBasePrice(this.value,product);
});

// function - set the base price
function setBasePrice(quantity,product){
    var basePrice = quantity * getTierPrice(quantity,product);
    // show the base price to user
    $('.basePrice').empty().val(basePrice);
};

// function - set price based on quantity and product
function getTierPrice(quantity,product){
    // switches through 6 tiers (based on quantity)
    // e.g. - If product is x and quantity is y, price = z
    // returns a value
};

There are many more working parts, but I wrote this to illustrate how it stacks up. It just seems verbose and a lot of moving parts. When the time comes to upgrade it, it’s a pain.

, - ? 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

, , YUI? , , ? , , . , . , .. .

+3
5

" " ( , JS), : ?

, , , , , "". , .

, , , "foo" "setBar", "" . "" . , foo setFooBar ( "setBar" "" ), , "" .

, , , , , , .

+3

, MVC- - , , . - (CakePHP PHP, Rails Ruby, Django Python ..), JS.

. . Backbone ( jQuery ( , )).

+3

javascript, , oop- .
javascript , .
JavaScript, , ... , , (: script, ImgObjects, , ).
, , , , maby ( ), , javascript - !

+1

oop! , javascript , oop. , " JavaScript" o'reilly. - yahoo stoyan stefanov, , oop js, .

, , , , , , . , . , .

:

//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 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. , . , , .

+1

I would be less worried about OOP verses other than OOP than with refactoring - for example, if you have "setBasePrice" and "setXXX" and "setYYY" that are very similar, you should convert them to a generic call to "setAnything ()" . OOP can help in this task, but it does not have special significance to it.

0
source

Source: https://habr.com/ru/post/1786785/


All Articles