简体   繁体   English

在JavaScript环境中执行

[英]Execute within an environment in javascript

I would like to allow my user to execute a series of functions while applying a certain object as the environment. 我希望允许用户在将某个对象用作环境的同时执行一系列功能。 For example, I have some object which contains data and operations. 例如,我有一些包含数据和操作的对象。

environment = {

member1 = 0

operation1 = -> 
    member1 += 1
}

I want to allow the user to send commands into the environment as if it were the global object, without referencing it with this 我想允许用户将命令发送到环境中,就像它是全局对象一样,而无需this进行引用

ie

environment.evaluate("operation1()")

It would also be nice if I could create operations outside the environment, but allow them to be sent into this hypothetical 'evaluate' function. 如果我可以在环境之外创建操作,但允许将其发送到此假设的“评估”功能中,那也将是很好的。

Is it possible to build something like this? 有可能建立这样的东西吗? Does it have native javascript support? 它具有本机javascript支持吗?

changing now. 现在改变。 just realized what you needed 刚意识到你需要什么

this calls a member of the private methods object. 这将调用私有方法对象的成员。

 // create a closure which is your environment using the module pattern var environment = (function() { // local variables to the environment var member1 = 0; // public interface var methods = { operation: function(num) { return member1 += (num || 1); }, evaluate: function evaluate(name) { var rest = Array.prototype.slice.call(arguments, 1); // call your function by name passing in the rest of the arguments return typeof methods[name] === 'function' && methods[name].apply(this, rest); } } return methods; })(); console.assert(typeof member1 === 'undefined', 'member1 is out of scope'); console.assert((environment.evaluate("operation", 2) === 2), 'member1 === 2'); console.log(environment.evaluate("operation", 2)); 
 <script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script> 

old answer 旧答案

I just noticed you requested coffee script. 我刚刚注意到您要求提供咖啡脚本。 This is javascript you could use. 这是您可以使用的javascript。 I have never used coffee script but it shouldn't be hard to change to coffee and coffee compiles to js anyway. 我从未使用过coffee脚本,但是无论如何也要更改为coffee和将compile编译为js并不难。

The key is having a closure around the entire environment and using eval to modify the internal state. 关键是对整个环境进行封闭,并使用eval修改内部状态。 You are really better off having specific getters and setters to limit the api to just the things you allow, otherwise the end user has access to modify anything in the scope, so nothing is private. 最好使用特定的获取器和设置器将api限制为仅允许您使用的东西,否则最终用户有权修改范围中的任何内容,因此没有什么是私有的。

 // create a closure which is your environment using the module pattern var environment = (function() { // local variables to the environment var member1 = 0; // return the public interface return { // this function evals the code in the context of the environment evaluate: function evaluate(operation) { // eval a closure so anything you put in there is run and returned to the outer environment return eval('(function(){ return ' + operation + '})()'); } } })(); console.assert( typeof member1 === 'undefined', 'is member1 out of scope' ); console.log( environment.evaluate("++member1") ); <!-- begin snippet: js hide: false --> 
 <script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script> 

 var es6environment = (function() { let member1 = 0; const methods = { operation: (num = 1) => member += num, evaluate: (name, ...rest) => typeof methods[name] === 'function' && methods[name].apply(this, rest); } return methods; })(); 
 <script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script> 

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

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