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
值。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…