Home  >  Q&A  >  body text

javascript - What is the difference between these two ways of writing JS

function cs() {
    var _cs = {};    
    _cs .open_change_customer_p = function (url, url_param) {
        console.log(url);
        console.log(url_param);
        //do something
        ...
        ...
        block_enter_presss();
    };
    
    function block_enter_presss() {
        /**屏蔽bootstrap搜索框按enter会刷新,搜索框响应enter键 */
    }
    return _cs ;
}
function cs() {
    cs .open_change_customer_p = function (url, url_param) {
        console.log(url);
        console.log(url_param);
        //do something
        ...
        ...
        block_enter_presss();
    };
    
    function block_enter_presss() {
        /**屏蔽bootstrap搜索框按enter会刷新,搜索框响应enter键 */
    }
    return cs ;
}

Supplementary content:
I use
var a=new cs();
a.open_change_customer_p ();
on the web page.
Then cs only exposes the open_change_customer_p function
In the above two ways of writing, is it better to hang the method to be exposed in a new variable, or is it better to hang it directly under the function name?

扔个三星炸死你扔个三星炸死你2663 days ago945

reply all(6)I'll reply

  • 巴扎黑

    巴扎黑2017-07-05 10:56:15

    I see the former one more often, but what the heck is the latter one? .

    reply
    0
  • 漂亮男人

    漂亮男人2017-07-05 10:56:15

    Is the second one like this?
    _cs.open_change_customer_p = function (url, url_param){......}

    The difference between the two is that the second method can be deleted, and the first method defined with var cannot be deleted. Other than that, there seems to be no difference. Depending on personal habits, the readability of the first method may be It will be better

    reply
    0
  • ringa_lee

    ringa_lee2017-07-05 10:56:15

    Why not extract the static method and write a separate function, but create a new one every time?

    reply
    0
  • 阿神

    阿神2017-07-05 10:56:15

    ...The first usage:
    var cs1 = cs();
    var cs2 = cs();
    cs1 and cs2 are two different objects, pointing to different memory spaces
    And the second one will point to the same The space is the cs function itself...
    The problem that arises is that if you declare two variables, if you change one of them, the value of the other will change accordingly (var cs1 = cs() usage)

    If you want var cs1 = new cs(), the second type always controls the cs function itself... The cs there should be replaced by this

    The first one is good...because the second one is weird...

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:56:15

    The functions should all be able to achieve the same function

    1. The first one looks more comfortable

    2. The second one looks more comfortable

    If you feel uncomfortable with the first definition, you can define it like this

    function cs() {  
        this.open_change_customer_p = function (url, url_param) {
            console.log(url);
            console.log(url_param);
            block_enter_presss();
        };
        
        function block_enter_presss() {
            /**屏蔽bootstrap搜索框按enter会刷新,搜索框响应enter键 */
        }
    }
    console.log(new cs())

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-07-05 10:56:15

    Your second method can be achieved

    var a=new cs();
    cs.open_change_customer_p ();

    Such a call? I doubt it

    reply
    0
  • Cancelreply