JavaScript 使用来自字符串的动态参数实例化新对象

我想用来自外部字符串的动态参数实例化一个新对象。


这是代码:


const editorInstance = new Editor('#edit',

  {

    placeholderText: null,

    theme: 'dark',

    language: 'en',

    linkList:[{text: 'test',href: 'test',target: '_blank'}],

    events: {

      initialized: function () {

        const editor = this


        this.el.closest('form').addEventListener('submit', function (e) {

          jQuery('#gs_editor_content').hide();

          jQuery(this).append('<div class="loadingDiv">&nbsp;</div>');

          o.script

        });


        texta = jQuery('#myeditor').find('textarea');

        targetFile = texta.attr('rel');

        content = editor.$oel.val();


        e.preventDefault();


        var fd = new FormData(); 

        fd.append( 'name' ,targetFile);

        fd.append( 'html', editor.$oel.val() );

        $.ajax({

          url : 'http://localhost/Update',

          type : 'POST',

          data: fd,

          processData : false,

          contentType : false,

          async : false,

          success : function(data, textStatus, request) {}

        });


        jQuery('#myeditor').dialog("close");


      }

    }

  }

)

linkList当我收到从我的服务器收到的新列表时,我需要在实例化我的对象之前修改参数。


我尝试使用 eval 或 parseFunction,但遇到意外的标识符错误。


知道我怎么能做到这一点吗?


慕容3067478
浏览 311回答 3
3回答

ibeautiful

我想用来自外部字符串的动态参数实例化一个新对象。这是代码:const editorInstance = new Editor('#edit',&nbsp; {&nbsp; &nbsp; placeholderText: null,&nbsp; &nbsp; theme: 'dark',&nbsp; &nbsp; language: 'en',&nbsp; &nbsp; linkList:[{text: 'test',href: 'test',target: '_blank'}],&nbsp; &nbsp; events: {&nbsp; &nbsp; &nbsp; initialized: function () {&nbsp; &nbsp; &nbsp; &nbsp; const editor = this&nbsp; &nbsp; &nbsp; &nbsp; this.el.closest('form').addEventListener('submit', function (e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery('#gs_editor_content').hide();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery(this).append('<div class="loadingDiv">&nbsp;</div>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o.script&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; texta = jQuery('#myeditor').find('textarea');&nbsp; &nbsp; &nbsp; &nbsp; targetFile = texta.attr('rel');&nbsp; &nbsp; &nbsp; &nbsp; content = editor.$oel.val();&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; var fd = new FormData();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fd.append( 'name' ,targetFile);&nbsp; &nbsp; &nbsp; &nbsp; fd.append( 'html', editor.$oel.val() );&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : 'http://localhost/Update',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: fd,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processData : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; async : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : function(data, textStatus, request) {}&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; jQuery('#myeditor').dialog("close");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; })linkList当我收到从我的服务器收到的新列表时,我需要在实例化我的对象之前修改参数。我尝试使用 eval 或 parseFunction,但遇到意外的标识符错误。知道我怎么能做到这一点吗?

吃鸡游戏

如果我理解您的问题是正确的,您是否在将链接列表包含到新对象之前尝试更新它?如果是这种情况,您可以使用基于承诺的东西作为 fetch 来获取您的数据:Using Fetch by MDN documentation。您已经在使用 JS 类,所以我确定您已经了解浏览器兼容性或使用 Babel(如果浏览器兼容性对您很重要)。如果您更喜欢使用 jQuery AJAX 来获取数据,您可以使用 async await 在您的软件实例化对象之前更新 linkList。Async Await 的 MDN 文档。在这种情况下,您可以执行以下操作:async function getData(paramIfNeeded) {&nbsp; var linkListData = await linkListFunction(paramIfNeeded2);&nbsp; return linkListData}function linkListFunction(paramIfNeeded2){&nbsp; //Your favourite data fetching function}getData().then((result) =>{&nbsp; var linkList = result;&nbsp; //instanciate the object});如果您更喜欢 ES 2015,可以这样做:function callData(){&nbsp; getData(paramIfNeeded)&nbsp; .then(result => {&nbsp; &nbsp; linkListFunction(paramIfNeeded2)&nbsp; &nbsp; .then( result => {&nbsp; &nbsp; &nbsp; //do things with linkList&nbsp; &nbsp; });&nbsp; });}

MYYA

问题可能是由异步引起的。您的班级正在从您的服务器收到响应之前初始化。您需要在收到响应后初始化类。例如const editorInstance = new Editor("#edit", {&nbsp; &nbsp; placeholderText: null,&nbsp; &nbsp; theme: "dark",&nbsp; &nbsp; language: "en",&nbsp; &nbsp; linkList: serverExample() // undefined});function serverExample() {&nbsp; &nbsp; setTimeout(() => {&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { text: "test", href: "test", target: "_blank" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { text: "test2", href: "test", target: "_blank" }&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }, 3000);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript