最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - What is NEGATIVE_INFINITY for in Javascript - Stack Overflow

programmeradmin1浏览0评论

I got this in a function I found on internet, but I can't understand what it's for. What I've read on W3school. Here is how they explain it:

Negative infinity can be explained as something that is lower than any other number.

So this is something I can understand but I can't think of a moment where you'd need this kind of constant. Plus, check this function out:

function setEqualHeight(selector, triggerContinusly) {

  var elements = $(selector)
  elements.css("height", "auto")
  var max = Number.NEGATIVE_INFINITY;

  $.each(elements, function(index, item) {
      if ($(item).height() > max) {
          max = $(item).height()
      }
  })

  $(selector).css("height", max + "px")

  if (!!triggerContinusly) {
      $(document).on("input", selector, function() {
          setEqualHeight(selector, false)
      })

     $(window).resize(function() {
          setEqualHeight(selector, false)
     })
  }
}

I understand the whole function. But I can't figure out why would max be = Number.NEGATIVE_INFINITY at first. Then, he check if the height is higher then the smallest possible number?

This function works perfectly so I'm guessing it's correct but I really don't understand what is the usage in this function or why would people use this in general.

I hope you guys will be able to enlighten me !

I got this in a function I found on internet, but I can't understand what it's for. What I've read on W3school.. Here is how they explain it:

Negative infinity can be explained as something that is lower than any other number.

So this is something I can understand but I can't think of a moment where you'd need this kind of constant. Plus, check this function out:

function setEqualHeight(selector, triggerContinusly) {

  var elements = $(selector)
  elements.css("height", "auto")
  var max = Number.NEGATIVE_INFINITY;

  $.each(elements, function(index, item) {
      if ($(item).height() > max) {
          max = $(item).height()
      }
  })

  $(selector).css("height", max + "px")

  if (!!triggerContinusly) {
      $(document).on("input", selector, function() {
          setEqualHeight(selector, false)
      })

     $(window).resize(function() {
          setEqualHeight(selector, false)
     })
  }
}

I understand the whole function. But I can't figure out why would max be = Number.NEGATIVE_INFINITY at first. Then, he check if the height is higher then the smallest possible number?

This function works perfectly so I'm guessing it's correct but I really don't understand what is the usage in this function or why would people use this in general.

I hope you guys will be able to enlighten me !

Share Improve this question edited Sep 10, 2015 at 21:14 Will Richardson 7,9707 gold badges43 silver badges58 bronze badges asked Sep 10, 2015 at 20:43 Yann ChabotYann Chabot 4,8793 gold badges43 silver badges58 bronze badges 3
  • 4 He could equally have started with 0. -Infinity is just a value that surely is smaller than every height, so it's a sensible start value to determine the maximum. – Bergi Commented Sep 10, 2015 at 20:48
  • 2 developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – j08691 Commented Sep 10, 2015 at 20:48
  • 1 You didn't get in developer.mozilla/en-US/docs/Web/JavaScript/Reference/…? Change your search engine. – Bergi Commented Sep 10, 2015 at 20:49
Add a ment  | 

2 Answers 2

Reset to default 6

It's mon to use extreme values as a starting point for functions finding the maximum in a set of data. In this case, because elements don't have a height less than 0, it isn't necessary, but provides better understanding once you've seen this concept before.

Consider the following arbitrary example.

var data = [ 1, 5, -10000, 50, 1240 ];

function maxData(data) {
    var max = Number.NEGATIVE_INFINITY;

    for (var i = 0; i < data.length; ++i)
        if (data[ i ] > max)
            max = data[ i ];

    return max;
}

This works great in JavaScript because any value pared to Math.NEGATIVE_INFINITY will be greater.

In other languages like C++, it's mon to use the minimum value of an integer, float, etc.

Negative Infinity is not a Rocket Science. Its a reverse of positive infinity.

var i=-3/0;
Ans= -Infinity
发布评论

评论列表(0)

  1. 暂无评论