我已经找到一个答案,但它不足以扩展我的知识库。
我在StackOverflow上搜索了很长时间x = x || y, z
的含义。
我找到了这个链接。 What does the construct x = x || y mean?
但是问题是, z
是什么意思?
我经常看到这些表达式
window.something = window.something || {}, jQuery
我已经知道如果第一个参数返回false
,则{}
将被分配给something
属性。
我的问题是,, jQuery
是什么意思?
有人能启发我并向我灌输这个非常重要的知识吗?
更新 2014年8月11日
所以我尝试进行测试。
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
并且它与这个相同。
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
另外一个。
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
改变
(foo = w || x || y, z)
中 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
我认为在函数的 }
后面放置变量,与声明一个新变量是一样的。
另一个测试。
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
然而,在像这样的场景中。 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
}
似乎如果去掉
jQuery
,它只会记录“Bacon Strips”。请参考此链接(另一个JSFiddle链接)(已删除jQuery
)。我真的不太明白这个...但是为什么在函数的
}
之后的( )
中有jQuery
,当jQuery库已经包含时,它算作完全运行代码的参考?从代码中删除
$.trim
后,它似乎又正常工作了。但我仍然不明白这个引用是如何工作的。
(没有, jQuery
和$.trim
的JSFiddle链接)
var p = x || y, z
以声明2个本地变量...但正如 user2864740 所说,这只是两个不同的语句。 - Arun P Johnyvar x = x || y, z
... - johnwaitjQuery
参数。你应该增加上下文信息。 - Ry-var p = x || y, z
可以等同于var p = x || y
和var z
(两个独立的定义)。z
的值仍然为“undefined”。 它类似于类似于var foo ='bar',bar ='foo',you ='see?';
的东西。 - scrowler