所以我遇到了一个答案,但这还不足以扩展我的知识库.
我一直在寻找x = x || y, zStackOverflow中的意义
我找到了这个. 构造x = x ||是什么 你的意思是?
但问题是什么, z呢?
我经常看到这些表达
window.something = window.something || {}, jQuery
我已经知道如果false在第一个参数上返回,那么{}将被分配给该something属性.
我的问题是,有什么, jQuery用?
有人可以启发我并用这个非常重要的知识吸引我吗?
更新 8/11/2014
所以我试着做试验.
var w = 0, x = 1,y = 2,z = 3;
var foo = w || x || y, z; //I see that z is a declared variable
console.log(foo); //outputs 1
Run Code Online (Sandbox Code Playgroud)
和它一样.
var w = 0, x = 1,y = 2;
var z = function(){return console.log("this is z");}
var foo = w || x || y, z; //same as this
console.log(foo); //still outputs 1
Run Code Online (Sandbox Code Playgroud)
另一个.
var w = 0, x = 1,y = 2;
var z = function(){return console.log("this is z");}
function foobar(){
this.bar = console.log(foo,z);
}(foo = w || x || y, z);
foobar(); //outputs 1 and string code of foobar
Run Code Online (Sandbox Code Playgroud)
改变z的值(foo = w || x || y, z).
var w = 0, x = 1,y = 2;
var z = function(){return console.log("this is z");}
function foobar(){
this.bar = console.log(foo,z);
}(foo = w || x || y, z=4);
foobar(); //outputs 1 and 4
Run Code Online (Sandbox Code Playgroud)
我假设在函数( )之后放置变量}与声明一个新变量相同.
另一个考验.
var w = 0, x = 1,y = 2,z = 1;
function foobar(){
var bar = 10,z=2;
console.log(z);
}(foo = w || x || y, z=4);
console.log(foo,z); // Seems that foo is public and made an output
foobar(); // outputs the z = 2 inside and disregards the z = 4 from (..., z=4)
console.log(z); // It seems that z is 4 again after calling foobar
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下.链接到JSFiddle
//Self-Executing Anonymous Function: Part 2 (Public & Private)
(function( skillet, $, undefined ) {
//Private Property
var isHot = true;
//Public Property
skillet.ingredient = "Bacon Strips";
//Public Method
skillet.fry = function() {
var oliveOil;
addItem( "\t\n Butter \n\t" );
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 ));
//Public Properties
console.log( skillet.ingredient ); //Bacon Strips
//Public Methods
skillet.fry(); //Adding Butter & Fraying Bacon Strips
//Adding a Public Property
skillet.quantity = "12";
console.log( skillet.quantity ); //12
//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 ));
try {
//12 Bacon Strips & 1 Cup of Grease
skillet.toString(); //Throws Exception
} catch( e ) {
console.log( e.message ); //isHot is not defined
}
Run Code Online (Sandbox Code Playgroud)
似乎如果你删除, jQuery它只记录"培根条"请参阅此链接链接到另一个JSFiddle(, jQuery已删除)
我真的没有得到这个..但是为什么在jQuery库已经被包含的情况下,函数的, jQuery内部计算为代码完全运行的参考?( )}
具有$.trim从代码中删除,这似乎再次正常工作.但我仍然不知道这种引用是如何工作的.
没有jQuery和$ .trim链接到JSFiddle