如何在脚本函数中获取var?

时间:2019-05-14 02:52:50

标签: javascript html alloy-ui

我想获取js函数值。我怎么得到他们?

我使用html和javascript以及Alloy-ui

我在chrome开发人员控制台中进行测试。 staY.diagrambuilder或staY.Y或创建函数名称。

<script>
 var staY = YUI().use(
    'aui-diagram-builder',

    function (Y) {
var diagrambuilder = new Y.DiagramBuilder({
            availableFields: availableFields,
            boundingBox: '#diagrambuilderBB',
            srcNode: '#diagrambuilderBB',
            fields: []
        });
console.log(diagrambuilder); //i want to access variable out of this script
});
</script>

如何在chrome开发人员控制台中获取diagrambuilder变量?

2 个答案:

答案 0 :(得分:1)

您只能访问传递给diagrambuilder的回调函数中的YUI.use

<script>
  var staY = YUI().use(
    'aui-diagram-builder',

    function (Y) {
      var diagrambuilder = new Y.DiagramBuilder({
        availableFields: availableFields,
        boundingBox: '#diagrambuilderBB',
        srcNode: '#diagrambuilderBB',
        fields: []
      });

      console.log(diagrambuilder);
      // use diagrambuilder here
    });
  );
</script>

如果您不想将所有代码都放在YUI.use函数中,则可以制作自己的函数,将其引入diagrambuilder

<script>
  var buildDiagram = function(diagrambuilder) {
    console.log(diagrambuilder);
    // use diagrambuilder here
  }

  var staY = YUI().use(
    'aui-diagram-builder',

    function (Y) {
      var diagrambuilder = new Y.DiagramBuilder({
        availableFields: availableFields,
        boundingBox: '#diagrambuilderBB',
        srcNode: '#diagrambuilderBB',
        fields: []
      });

      buildDiagram(diagrambuilder);
    });
  );
</script>

您还有另一个选择是创建一个接受Y的函数:

<script>
  var buildDiagram = function(Y) {
    var diagrambuilder = new Y.DiagramBuilder({
      availableFields: availableFields,
      boundingBox: '#diagrambuilderBB',
      srcNode: '#diagrambuilderBB',
      fields: []
    });

    console.log(diagrambuilder);
    // use diagrambuilder here
  }

  var staY = YUI().use('aui-diagram-builder', buildDiagram);
</script>

所有这些都做完全相同的事情,但是代码的编写位置不同。无论哪种方式,关键是您只能在Y.DiagramBuilder回调函数中访问YUI().use()

不能做这样的事情:

<script>
  var staY = YUI().use(
    'aui-diagram-builder',

    function (Y) {
      var diagrambuilder = new Y.DiagramBuilder({
        availableFields: availableFields,
        boundingBox: '#diagrambuilderBB',
        srcNode: '#diagrambuilderBB',
        fields: []
      });
    });
  );

  console.log(diagrambuilder); // undefined
</script>

不能这样做:

<script>
  var diagrambuilder;

  var staY = YUI().use(
    'aui-diagram-builder',

    function (Y) {
     diagrambuilder = new Y.DiagramBuilder({
        availableFields: availableFields,
        boundingBox: '#diagrambuilderBB',
        srcNode: '#diagrambuilderBB',
        fields: []
      });
    });
  );

  // This will most likely be null, as it takes time to load
  // the aui-diagram-builder library, and the code is asynchronous.
  // If this does happen to work for you, know that you're getting lucky
  // and it will fail at some point. This is not something you should do,
  // but I think it's what you're hoping for, so I'm pointing it out.
  console.log(diagrambuilder);
</script>

答案 1 :(得分:1)

<script>
var diagrambuilder = null;
var staY = YUI().use(
    'aui-diagram-builder',

    function (Y) {
diagrambuilder = new Y.DiagramBuilder({
            availableFields: availableFields,
            boundingBox: '#diagrambuilderBB',
            srcNode: '#diagrambuilderBB',
            fields: []
        });
console.log(diagrambuilder); //i want to access variable out of this script

diagramInitialized();

});

function diagramInitialized() {
    console.log('If you are seeing this thenm diagrambuilder is available in global scope'); //it is accessible now;
    console.log(diagrambuilder); //it is accessible now;
}
</script>

这是您想要的吗? @ user9041996