开发者

Found this in the jquery source, how does it work?

All this is one expression. What exactly gets put inside div in the end? What's it called when you have a bunch of co开发者_如何学编程mma delimited expressions?

var div = document.createElement( "div" ),
    documentElement = document.documentElement,
    all,
    a,
    select,
    opt,
    input,
    tds,
    events,
    eventName,
    i,
    isSupported;


It is just the declaration of a bunch of variables inside a function scope. The first two variables are also initialized with their declaration. You can declare/initialize multiple variables at once by separating each with a comma like is done here.

In other uses, the comma character is an operator in javascript. From MDN: The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

The variable div is initialized here to an empty div tag. There is nothing inside it yet until later code uses it. The code following this in jQuery, puts some content into that div and then runs a series of test operations on it to do feature tests and see what features are supported in the current browser.


To first answer:

var div = document.createElement( "div" ),
    documentElement = document.documentElement,
    all,
    a,
    select,
    opt,
    input,
    tds,
    events,
    eventName,
    i,
    isSupported;

is the same as:

var div = document.createElement( "div" );
var documentElement = document.documentElement;
var all;
var a;
var select;
var opt;
var input;
var tds;
var events;
var eventName;
var i;
var isSupported;


This is just the definition of some variables. In JavaScript you can define and/or initialize variables without having to write the var-statement for every variable like this.

var x=0, y=1, array=[], var1, var2;

So this is basically what is happening here.


It's called a series of variable declarations. It's just like writing:

var div = document.createElement( "div" );
var documentElement = document.documentElement;
var all;
var ...

So they are unrelated, the other variables have nothing to do with the div.


What exactly gets put inside div in the end?

The return value of document.createElement( "div" ).

What's it called when you have a bunch of comma delimited expressions?

The comma is just an operator. It returns the right hand side, but it has lower precedence that = and noting to the left ever captures the value.

This is just a way of avoiding typing var over, and over, and over…

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜