javascript - What is the difference between these two ways of writing JS
扔个三星炸死你
扔个三星炸死你 2017-07-05 10:54:15
0
6
946
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?

扔个三星炸死你
扔个三星炸死你

reply all(6)
巴扎黑

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

漂亮男人

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

ringa_lee

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

阿神

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

给我你的怀抱

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())
phpcn_u1582

Your second method can be achieved

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

Such a call? I doubt it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!