简体   繁体   English

我可以将多个JavaScript Object方法保存为变量吗?

[英]Can I save multiple JavaScript Object methods as a variable?

I am writing an extension for a text-editor (Brackets) that can generate HTML and append libraries automatically in the HTML. 我正在编写文本编辑器(括号)的扩展程序,该扩展程序可以生成HTML并自动在HTML中附加库。

I have an Object called 'choice'. 我有一个名为“选择”的对象。

This modal requests the users input: 该模式要求用户输入: 伊姆古尔

choice grabs the user's input by defining methods on choice 选择通过定义选择方法来获取用户输入

partial JS here: 部分JS:

var choice = new Object();

    choice.language = function () {
        //Buid HTML top 'head'
        var htmlTop = "<!DOCTYPE html>" + "<html>" + "<head lang='";
        //Grab Selected Language Type
        var languageChoice = document.getElementById("languages").value;
        //Determine what Selected Language type is and complete HTML 'head'
        if (languageChoice === "english") {
          languageChoice = "en";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "german") {
          languageChoice = "de";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "spanish") {
          languageChoice = "es";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "french") {
          languageChoice = "fr";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "italian") {
          languageChoice = "it";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "chinese") {
          languageChoice = "zh-cn";
          return htmlTop + languageChoice + "'>";
        }
      }; //end choice.language

    choice.charset = function () {
        //Build meta and the rest of the 'head tag'
        var htmlCharset_Beginning = "<meta charset='";
        var htmlCharset_End = "'>" + "<title> -Insert Title- </title>" + "<!-- Insert CSS links below -->" + "</head>" + "<body>";
        var charsetChoice = document.getElementById("charset").value;
        if (charsetChoice === "utf8") {
          charsetChoice = "UTF-8";
          return htmlCharset_Beginning + charsetChoice + htmlCharset_End;
        } else {
          charsetChoice = "UTF-16";
          return htmlCharset_Beginning + charsetChoice + htmlCharset_End;
        }
      }; // end choice.charset

    choice.doctype = function () {
      var doctypeChoice = document.getElementById("doctype").value;
      return doctypeChoice;
    }; // end doctype

    choice.libraries = function () {
      var checkedBoxes = getCheckedBoxes("lib_checkboxes");
      checkedBoxes.forEach(function(item){
      var scripts =+ $(item).data('script');

   });//End forEach
      var bottomHTML = scripts + "</body>" + "</html>";
      return bottomHTML;
    }; //End choice.libraries


    var chosenTemplate = function(){
    var template = choice.language() + choice.charset() + choice.libraries();

      // insert html into file, this will overwrite whatever content happens to be there already
      EditorManager.getCurrentFullEditor()._codeMirror.setValue(template);

      // automatically close the modal window
      $('#templates_modalBtn').click();
    };

    //Get checkedBoxes function
    // Pass the checkbox name to the function
    function getCheckedBoxes(chkboxName) {
      var checkboxes = document.getElementsByName(chkboxName);
      var checkboxesChecked = [];
      // loop over them all
      for (var i = 0; i < checkboxes.length; i++) {
        // And stick the checked ones onto an array...
        if (checkboxes[i].checked) {
          checkboxesChecked.push(checkboxes[i]);
        }
      }
      // Return the array if it is non-empty, or null
      return checkboxesChecked.length > 0 ? checkboxesChecked : null;
    }


  } // End action();

  //JEFF STOP CODING HERE

  // Register the commands and insert in the File menu
  CommandManager.register(Strings.MENU_COMMAND, 'templates', action);
  var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
  menu.addMenuDivider();
  menu.addMenuItem('templates');

}); //end define;

QUESTION: 题:

Can I save multiple methods (each method returns a string) as a variable? 我可以将多个方法(每个方法返回一个字符串)另存为变量吗?

Example here: 这里的例子:

  var chosenTemplate = function(){
    var template = choice.language() + choice.charset() + choice.libraries();

      // insert html into file, this will overwrite whatever content happens to be there already
      EditorManager.getCurrentFullEditor()._codeMirror.setValue(template);

      // automatically close the modal window
      $('#templates_modalBtn').click();
    };

My code is loading with no errors, but its not executing at all so I am trying to debug and figure out what is going wrong... 我的代码正在加载,没有错误,但是根本没有执行,所以我正在尝试调试并弄清楚出了什么问题。

Before you realize the function 'chosenTemplate', you should check whether the document stream of the page has already downloaded. 在实现功能“ chosenTemplate”之前,应检查页面的文档流是否已下载。 If it not, you may not be able to get the value of the widget (empty). 如果不是,您可能无法获取小部件的值(空)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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