Cleanest format for writing javascript objects

前端 未结 4 510
面向向阳花
面向向阳花 2021-02-06 19:20

What is the cleanest format for writing javascript objects?

Currently I write mine in the following format

if (Namespace1 == null) var Namespace1 = {};
i         


        
4条回答
  •  灰色年华
    2021-02-06 20:15

    I use the following function:

    jQuery.namespace = function() {
        var a = arguments, o = null, i, j, d;
        for (i=0; i

    Then I can use this function to create a namespace just like this:

    $.namespace("jQuery.namespace1");
    

    Once I've created the namespace I can declare functions or whatever you want inside it:

    a function:

    $.namespace1.asyncRequest = function() {
        [function body]
    };
    

    a constant:

    $.namespace1.OFFSET = 10;
    

    an object:

    $.namespace1.request = { requestId: 5, protocol: 'JSON' };
    

    I think it's simple and elegant :-)

    Bye, Alex

提交回复
热议问题