Javascript对象将代码作为字符串获取 [英] Javascript object get code as string

查看:98
本文介绍了Javascript对象将代码作为字符串获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,如果这是重复的话,我很抱歉,但每当我搜索对象和代码时,我都会收到教程页面。

First off, I am sorry if this is a duplicate, but every time I googled for 'object' and 'code' I got tutorial pages.

我想要知道是否有任何简单的方法来获取与对象关联的代码。类似

I want to know if there is any easy way to get the code associated with an object. Something like

function A(){
  this.name = 'Kaiser Sauze';
}
a = new A();
console.log(a.displayCode());
//OUTPUT
"function A(){ this.name = 'Kaiser Sauze';}"

我希望能够在浏览器中查看代码,修改代码并重新加载函数。我想知道是否有某种方法可以做到这一点,或者我是否必须通过这样做来填充泵:

I want to be able to view the code, modify it and reload the function, all from within the browser. I wanted to know if there was some way to do this, or if I have to prime the pump by doing something like this:

function A(){
  this.name = 'Kaiser Sauze';
  this.code = "function A(){ this.name = 'Kaiser Sauze';}"
}

然后每次用户加载文本编辑器以查看 this.code 我连接onchange以更新这个.code

then every time the user loads up the text editor to view this.code I connect the onchange to update this.code.

编辑

结果yankee建议一个简单的解决方案

turns out yankee suggested a simple solution to this

function A(x){
  this.x = x ;
}
console.log(A.toString());
//OUTPUT
"function A(x){
  this.x = x ;
}"

但在我的实现中,变量'x'可以是一个函数(实际上是一个带有变量,函数和子对象的复杂对象,我通过调用dojo.mixin混合它) ,所以我真正想要的是在实例化时知道代码,类似的东西

but in my implementation the variable 'x' can be a function (actually a complicated object with variables, functions and sub objects which I mix in via a call to dojo.mixin), so what I really want is to know the code when instantiated, something like so

function A(x){
  this.x = x ;
}
var a = new A(function(){/*DO SOMETHING*/);
console.log(a.toString());
//OUTPUT
"var a = new A(function(){/*DO SOMETHING*/);"

但是,正如你们大多数人已经知道的那样,输出的所有东西都是对象。通过将初始化放在这样的函数中,我几乎找到了解决方法。

but, as most of you already know, all that gets output is something like "Object". I have almost found a way around this, by putting the initialization in a function like so

function A(x){
  this.x = x ;
}
function _A(){
  var a = new A(function(){/*DO SOMETHING*/);
}
console.log(_A.toString());
//OUTPUT
"function _A(){
  var a = new A(function(){/*DO SOMETHING*/);
}"

但这很令人困惑,然后我必须进入并开始解析我不想做的字符串。

but that is confusing, and then I have to go in and start parsing the string which I do not want to do.

编辑:我问这一切的原因是b / c我想制作既动态可执行又高度模块化的代码。我正在处理画布。我希望用户能够单击例如矩形,查看其代码,然后修改然后加载/执行它。我有一系列规则,但基本上我有一个形状类,定义该形状的一切(颜色,透明度,填充,笔画......)必须作为参数传递给对象cosntructor,如:

The reason I ask all of this is b/c I want to make code that is both dynamically executable and highly modular. I am dealing with the canvas. I want the user to be able to click on a, for example, rectangle, view its code, and modify and then load/execute it. I have a series of rules but basically I have a shape class and everything that defines that shape (color, transparency, fills, strokes...) has to get passed as a parameter to the object cosntructor, something like:

rect = new Shape({color : 'rgba(0,0,0,1)' , 
  x : 0 , 
  y : 0 , 
  w : 100 , 
  h : 100 ,
  draw : function() {ctx.fillStyle = this.color;
    ctx.fillRect(this.x,this.y,this.w,this.h);
  }
});

这样代码是自动模块化的,我不必担心颜色是在页面的顶部,然后高度被定义在页面的一半,依此类推。现在我唯一需要的是以某种方式,作为参数传递,整个上面的字符串表示初始化。我可以将它包装在一个函数中并调用toString,就像这样

This way the code is automatically modular, I don't have to worry about the color being defined at the top of the page, and then the height being defined half way down the page, and so on. Now the only thing I need is to somehow, pass as a parameter, the entire above string representation of the initialization. I could wrap it in a function and call toString on that, like so

function wrapper(){
  rect = new Shape({color : 'rgba(0,0,0,1)' , 
        x : 0 , 
        y : 0 , 
        w : 100 , 
        h : 100 ,
        draw : function() {ctx.fillStyle = this.color;
          ctx.fillRect(this.x,this.y,this.w,this.h);
        },
        code : wrapper.toString()
      });
  }

但是有两个问题。 1)我必须手动删除函数包装器()并尾随} 以及向左移动每一行一个标签。 2)不保证用户会记得包含包装函数,因为它对于绘图来说是完全不必要的。我试图想一个包装器看似自然的方式,但我想不出任何方法。但是我又睡了30多个小时。

but then there are two problems. 1) I have to manually remove the function wrapper() and trailing } as well as moving every line to the left by one tab. 2) there is no guarantee that a user will remember to include the wrapper function as it is totally unecessary for purposes of drawing. I am trying to think of a way where the wrapper would seem natural, but I can't think of any. But then again I haven't slept in over 30 hours.

推荐答案

我简直不敢相信没人提出这个问题(我道歉)如果这个答案在某些地方之间)。我没有想到它,因为在开发时,我的所有工作都是客户端。我真正需要做的就是使用Ajax作为javascript加载代码一次。加载并创建对象后,我再次将其作为字符串加载并将其分配给对象中的变量。

I can't believe no one suggested this (I apologize if this answer is somewhere in between the lines). I didn't think of it because at the time of development, all my work was clientside. All I really have to do is load the code once with Ajax as javascript. Once it is loaded and an object created, I load it again as a string and assign it to a variable in the object.

这篇关于Javascript对象将代码作为字符串获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆