Javascript模块编程

Javascript module programming

本文关键字:编程 模块 Javascript      更新时间:2023-09-26

我正在学习模块编程。看看下面的代码:

var goodsspec = function(){
    function setSpec(){
        var
        _price,
        _storage,
        defaultstats = true,
        _val = '',
        _resp = {
                    storage:".goods_stock",
                    price:".price"
                }
        $(".sys_item_spec .sys_item_specpara").each(function(){
            var i = $(this);
            var v = i.attr("data-attrval");
            if(!v){
                      defaultstats = false;
            }else{
                      val += _val!=""?"":"";
                      _val += v;
                 }
        });
        if(!!defaultstats){  
            _storage = sys_item['sys_attrprice'][_val]['goods_storage'];
            _price = sys_item['sys_attrprice'][_val]['price'];
        }else{
                _storage = sys_item['goods_storage'];
                _price = sys_item['price'];
             }
        $(_resp.storage).text( _storage);
        $(_resp.price).text( _price);
        if ( _storage == 0){
                // Waring
            }
        }
        return {
            set:function(){
               return setSpec();
            }
        };
    }();
    console.log(goodsspec.price);

我希望在选择项目时获得价格和存储属性值_value。我该怎么做?

尝试类似的东西

   var Item = new Object();
   function setSpec(){
     // setting code
     Item.price = _price;
     Item.storage = _storage
   }

然后你可以使用

console.log("Prices is "+Item.price+" for "+Item.storage+".");