开发者

Write another language above Javascript VM?

开发者 https://www.devze.com 2023-01-17 05:06 出处:网络
I wonder if it\'s possible to write a new language above Javascript VM so that you could embed y开发者_如何学Pythonour language - which is based on javascript - and it will work together with native j

I wonder if it's possible to write a new language above Javascript VM so that you could embed y开发者_如何学Pythonour language - which is based on javascript - and it will work together with native javascript.

Example:

var person_exists = true; // native js

Animal eats banana // my language

console.log(person_exists) // native js

I've seen Cappuccino's syntax and they say it could be run without compilation in the browser.

But then I have seen Coffeescript's syntax, and that one has to be compiled first to javascript.

So is it possible to create a new language/javascript syntax on top of Javascript VM?

If yes, are there tutorials/books for how to do this?


Processing.js is a script language interpreted by Javascript (interpreted, not compiled to JS as far as I know), you could take a look at that approach. Processing.js is specifically tailored to produce graphics onto a webpage, so it may not be exactly what you need, but it is an example of having two scripting languages on one website.

(Processing is a separate graphics language which was first run in a Java environment (as well as other implementations), before the Canvas element was conceived and another implementation based on Javascript was made.)

What it seems you want is 'Javascript with my own features'. This is only possible if you (or someone else) build a JS+yourfeatures interpreter on top of Javascript. I don't think that's a very plausible thing to do though. Processing.js's solution is probably a good middle ground, where you separate your own language from actual Javascript - that is, if your language and the regular JS can be separated.

<script language="javascript">
  var person_exists = true; // native js
  interpret("animal"); // your language interpreter. Interpret function looks through the DOM for a script tag with the "myscript" language and the 'animal' ID.
  console.log(person_exists) // native js
</script>
<script language="myscript" id="animal">
   Animal eats banana // your language
</script>

Still, I'd reccommend thinking about what you ''really'' want before skipping to the "I know, I'll develop my own language!" part. What's wrong with

Animal = {
   eats: function(fruit) {
       console.log('om nom nom nom ' + fruit);
   }
};

var person_exists = true;
Animal.eats(banana);
console.log(person_exists);

for example?


Sure, see GWT or Parenscript for some quite different examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消