Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中声明名称空间?(How do I declare a namespace in JavaScript?)

How do I create a namespace in JavaScript so that my objects and functions aren't overwritten by other same-named objects and functions?

(如何在JavaScript中创建名称空间,以使我的对象和函数不会被其他同名对象和函数覆盖?)

I've used the following:

(我使用了以下内容:)

if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}

Is there a more elegant or succinct way of doing this?

(有没有更优雅或更简洁的方法?)

  ask by Scott McKenzie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I use the approach found on the Enterprise jQuery site :

(我使用在Enterprise jQuery网站上找到的方法 :)

Here is their example showing how to declare private & public properties and functions.

(这是他们的示例,显示了如何声明私有和公共属性和功能。)

Everything is done as a self-executing anonymous function.

(一切都作为一个自执行的匿名函数完成。)

(function( skillet, $, undefined ) {
    //Private Property
    var isHot = true;

    //Public Property
    skillet.ingredient = "Bacon Strips";

    //Public Method
    skillet.fry = function() {
        var oliveOil;

        addItem( "
 Butter 
" );
        addItem( oliveOil );
        console.log( "Frying " + skillet.ingredient );
    };

    //Private Method
    function addItem( item ) {
        if ( item !== undefined ) {
            console.log( "Adding " + $.trim(item) );
        }
    }
}( window.skillet = window.skillet || {}, jQuery ));

So if you want to access one of the public members you would just go skillet.fry() or skillet.ingredients .

(因此,如果您想访问其中一个公共成员,则只需进入skillet.fry()skillet.ingredients 。)

What's really cool is that you can now extend the namespace using the exact same syntax.

(真正酷的是,您现在可以使用完全相同的语法扩展名称空间。)

//Adding new Functionality to the skillet
(function( skillet, $, undefined ) {
    //Private Property
    var amountOfGrease = "1 Cup";

    //Public Method
    skillet.toString = function() {
        console.log( skillet.quantity + " " +
                     skillet.ingredient + " & " +
                     amountOfGrease + " of Grease" );
        console.log( isHot ? "Hot" : "Cold" );
    };
}( window.skillet = window.skillet || {}, jQuery ));

The third undefined argument(第三个undefined参数)

The third, undefined argument is the source of the variable of value undefined .

(第三个undefined参数是值undefined的变量的来源。)

I'm not sure if it's still relevant today, but while working with older browsers / JavaScript standards (ecmascript 5, javascript < 1.8.5 ~ firefox 4), the global-scope variable undefined is writable, so anyone could rewrite its value.

(我不确定今天是否仍然有用,但是在使用较旧的浏览器/ JavaScript标准(ecmascript 5,javascript <1.8.5?firefox 4)时,全局变量undefined是可写的,因此任何人都可以重写其值。)

The third argument (when not passed a value) creates a variable named undefined which is scoped to the namespace/function.

(第三个参数(未传递值时)创建一个名为undefined的变量,其作用域为名称空间/函数。)

Because no value was passed when you created the name space, it defaults to the value undefined .

(因为在创建名称空间时没有传递任何值,所以它默认为undefined值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...