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

how can I get var in the script function?

我想获取js函数值。我怎样才能得到它们?

我用html和javascript和alloy-ui

我在 chrome 开发者控制台中测试。 staY.diagrambuilder or staY.Y or make function name.

<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 变量?

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

<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>

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

不能做这样的事情:

<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>
<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