javascript – 逗号在构造中的含义是什么(x = x || y,z)?

前端之家收集整理的这篇文章主要介绍了javascript – 逗号在构造中的含义是什么(x = x || y,z)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我遇到了一个答案,但这还不足以扩展我的知识库.

我一直在寻找x = x ||的年龄y,z表示StackOverflow

我找到了这个.
What does the construct x = x || y mean?

但问题是,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

和它一样.

var w = 0,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,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,z=4);
foobar(); //outputs 1 and 4

我假设在函数的}之后将变量放在()中与声明一个新变量相同.

另一个考验.

var w = 0,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

但是,在这种情况下. Link to 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它只记录“培根条”
请参阅此链接Link to another JSFiddle(,jQuery已删除)

我真的没有得到这个..但是为什么在一个函数之后的()里面的jQuery作为代码的参考,当jQuery库已经包含在内时,它会完全运行?

将$.trim从代码删除后,它似乎再次正常工作.但我仍然不知道这种引用是如何工作的.
Link to the JSFiddle without the,jQuery and $.trim

解决方法

JavaScript中的 Comma Operator评估操作数并返回最后一个(最右边)的值.通过JS Operator Precedence,将首先评估OR操作,然后进行分配.

所以这个表达式x = x || y,z有效(x =(x || y)),z. OR运算符将返回比较的布尔结果,或者对于非布尔类型,返回第一个操作数(如果它是真值),否则返回第二个操作数.赋值运算符的优先级也高于逗号运算符,因此x将被赋予OR返回的值. z的值不会对OR运算或赋值产生任何影响.实际上,它最后会被评估,这意味着它本质上是一个单独的语句,对该表达式中的任何其他内容都没有影响.我没有看到以这种方式编写表达式的任何实际价值.

原文链接:https://www.f2er.com/js/157915.html

猜你在找的JavaScript相关文章