简体   繁体   中英

Can someone explain this return statement for me?

I have a return statement that was used in this Stackover answer , that I can't quite understand. Here it is:

return maxWidth > $this.width() || maxHeight > $this.height();

What does it mean to return something in way?

I'll edit the title of this question after an answer as soon as I know what it is :)

It's the equivalent of:

if (maxWidth > $this.width() || maxHeight > $this.height()) {
  return true;
} else {
  return false;
}

In other words, if either maxWidth is greater than the width() of $this or maxHeight is greater than the height() of $this , it will return true ; otherwise, it will return false .

It returns boolean .

return maxWidth > $this.width() || maxHeight > $this.height();

Assume,

maxWidth = 300 
$this.width() = 200
maxHeight = 400
$this.height() = 500

so it returns

(300>200 || 400>500) ==> (T || F) ==> TRUE

在该特定示例中,代码正在检查最大子尺寸是否超过父尺寸,尺寸为宽度和高度。

It's known as short-circuit evaluation , and in this case will return a boolean value. If

maxWidth > $this.width() 

is true, it'll return true , without evaluating the second test. Otherwise it'll return the result of evaluating

maxHeight > $this.height(). 

It returns true if one of the dimension of $this , which is a jQuery wrapper object created as $(this) , is smaller than some variables.

In the code you link too, that enables the detection of overflowing as maxWidth and minWidth are the dimensions of the biggest child : if one child is bigger than this, then it is overflowing this.

Have a look at the width function.

其布尔值,因此,如果宽度或高度的最大值大于实际宽度,则为true。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM