Цепочка объема в JavaScript

Можно упростить эту проблему вполне немного при разделении каждого прямоугольника на меньшие прямоугольники. Соберите все координаты X и Y всех прямоугольников, и они становятся Вашими точками разделения - если прямоугольник пересекает строку, разделите его в два. Когда Вы сделаны, у Вас есть список прямоугольников, которые перекрывают или 0% или 100%, если Вы сортируете их, должно быть легко найти идентичные.

54
задан Neeraj Kumar 25 July 2019 в 09:34
поделиться

3 ответа

Не устанавливать DYLD_LIBRARY_PATH . Из-за этого env var, dyld динамического компоновщика, находит /opt/local/lib/libjpeg.dylib и т. Д. внутренние функции могут ссылаться на переменные, присутствующие в их внешних включающих функциях, даже после того, как их родительские функции уже выполнены.

JavaScript разрешает идентификаторы в конкретном контексте, проходя вверх по цепочке областей видимости, переходя от локального к глобальному.

Учтите это пример с тремя вложенными функциями:

var currentScope = 0; // global scope
(function () {
  var currentScope = 1, one = 'scope1';
  alert(currentScope);
  (function () {
    var currentScope = 2, two = 'scope2';
    alert(currentScope);
    (function () {
      var currentScope = 3, three = 'scope3';
      alert(currentScope);
      alert(one + two + three); // climb up the scope chain to get one and two
    }());
  }());
}());

Рекомендуемые чтения:

58
ответ дан 7 November 2019 в 08:02
поделиться

Any function call in ECMAScript ( core language that JS is based on ) produces a separate execution context, which run individually from one another. Inside of each execution context, this refers to the object in question, defaulting to whatever the function is attached to.

function foo() {
    alert(this===window)
}

Would alert true, because the window is the object which owns the 'foo' method. Any variables defined in a function become accessed through that function's unique scope chain, environment.

function world() {
    var name = 'global';
    alert(name)
}

would alert 'global' obviously.

function world() {
    var name = 'global';
    (function() {
        var name = 'country';
        alert(name)
    })();
    alert(name)
}

In the latest example, when the first alert is invoked, Javascript determines that in the scope chain of the inner function that the identifier name is defined, so it doesn't have to look up the scope chain to grab it.

In the second alert invocation, name is also defined in the same context and alerts 'global';

function world() {
    var name = 'global';
    (function() { alert(name) })();
}

In this example, the name identifier is not defined in the same context and thus it has to travel up the scope chain to the outer function where name is defined, and it alerts global.

Reference:

17
ответ дан 7 November 2019 в 08:02
поделиться

This is about closure. You may use variables outer from scope in the inner scope:

function get_inner_scope () {
    var outer = 'Outer variable value';
    return function () {
        alert(outer);
    }
}
f = get_inner_scope();
f(); // alerts Outer variable value

More deatailed info with other samples by first google's link: http://blogs.msdn.com/jscript/archive/2007/07/26/scope-chain-of-jscript-functions.aspx

5
ответ дан 7 November 2019 в 08:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: