MudBytes
» MUDBytes Community » Coding Discussions » Coding and Design » language written in javascript
Pages: << prev 1, 2 next >>
language written in javascript
JohnnyStarr
Wizard






Group: Members
Posts: 996
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#1 id:60183 Posted Jan 19, 2012, 2:37 pm

I've been reading up on how javascript can be used as an intermediary language.  CoffeeScript comes to
mind.  The disadvantage though, is that you must compile to javascript first.  I love jQuery as a good
javascript library, but I'm starting to think that it would be cool to create a new language in javascript
itself that would run in the browser.

I'm not too sure about speed, but I don't think that it would be too much of an issue if the language was
compiled directly into javascript. 

First of all, I would like to offer a more logical approach to DOM object access:

Raw
Code (javascript):
var txt = document.getElementById("thing").value;

jQuery
Code (javascript):
var txt = $("#thing").val();

New Language
Code (text):
txt = doc.thing

The point being that dom.thing would automatically access the value of the "thing" element.  If "thing"
happened to be a <div> then typing dom.thing would be equal to document.getElementById("thing").innerHTML

The only way to include the new language scripts that I can see, would be to use a div with an id that has a style set
by calling the language library.

Code (HTML):
<script type="text/javascript" src="new_language.js" />
// set's the style of new_language to: 'display: none;'

<div id="new_language">
alert doc.thing
</div>


Then, on load you could execute the script:

Code (HTML):
<body onload="new_lang_execute()">

Which looks for the id's innerHTML and does the rest for you.

This might be a dumb idea, but I think it would be fun to write.  If anyone has any reasons why this is a good idea please
share.  If it's a dumb idea, please explain. 
.........................
http://3.bp.blogspot.com/-RdaO2sGy6Jo/UXqmRANiPpI/AAAAAAAAARM/FkgyUR1PIuc/s1600/starrlab-logo.png

Last edited Jan 19, 2012, 2:44 pm by JohnnyStarr
Idealiad
Wizard




Group: Members
Posts: 924
Joined: Jan 28, 2007

Go to the bottom of the page Go to the top of the page
#2 id:60184 Posted Jan 19, 2012, 2:44 pm


JohnnyStarr said:
I've been reading up on how javascript can be used as an intermediary language.  CoffeeScript comes to
mind.  The disadvantage though, is that you must compile to javascript first.  I love jQuery as a good
javascript library, but I'm starting to think that it would be cool to create a new language in javascript
itself that would run in the browser.

I'm not too sure about speed, but I don't think that it would be too much of an issue if the language was
compiled directly into javascript
.


Bolded for emphasis...you lost me there.

JohnnyStarr
Wizard






Group: Members
Posts: 996
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#3 id:60185 Posted Jan 19, 2012, 2:45 pm

Ahh.

What I meant to say, is that the CoffeeScript compiler is a desktop application that does not run in the browser.
.........................
http://3.bp.blogspot.com/-RdaO2sGy6Jo/UXqmRANiPpI/AAAAAAAAARM/FkgyUR1PIuc/s1600/starrlab-logo.png

Last edited Jan 19, 2012, 2:45 pm by JohnnyStarr
plamzi
Wizard






Group: Members
Posts: 1,052
Joined: Nov 10, 2010

Go to the bottom of the page Go to the top of the page
#4 id:60187 Posted Jan 19, 2012, 4:25 pm

JohnnyStarr said:

The point being that dom.thing would automatically access the value of the "thing" element.  If "thing"
happened to be a <div> then typing dom.thing would be equal to document.getElementById("thing").innerHTML


Since you have written "doc.thing" and not "doc.thing()" or "doc.get('thing')", my comments assume that you'll be building a library of node references everytime the page loads.

In your example, how would you get a reference to a div object itself instead of its innerHTML?

Of course, you can store raw references to objects in another placeholder object, say obj.thing. But what if someone wants to get a div object by class name? Yet another placeholder object? For these placeholder objects to be available, you'd have to build them after the page loads fully but before any js needs them. That's a narrow window. And are you certain that the benefits from having these shortcuts would outweigh the permanent footprint on performance?

If you're confident that for your particular app, you'll always want the innerHTML whenever a div is referenced, then write your own one-line helper function to shorten things a bit more. But I don't think it's easy to outdo something like jQuery in terms of universal applicability. Some very sharp folks have been honing these libraries for many years now...
.........................
The MUD Portal web app, Bedlam for iPhone, Web & Telnet, MUDMaster for iPhone, Havoc - a custom node.js codebase

Last edited Jan 19, 2012, 4:30 pm by plamzi
Deimos
Sorcerer






Group: Members
Posts: 403
Joined: Mar 8, 2010

Go to the bottom of the page Go to the top of the page
#5 id:60193 Posted Jan 19, 2012, 5:23 pm

I don't understand the point in the syntax "doc.thing".  You seem to lose quite a bit of flexibility just to save a few keystrokes.  As plamzi pointed out, how do you do things like change the color of "thing"?  If "doc.thing" is its value, the you can't really do "doc.thing.color" or similar.
.........................
Any sufficiently advanced magic will run circles around technology.

Cratylus
Wizard






Group: Members
Posts: 1,931
Joined: May 22, 2006

Go to the bottom of the page Go to the top of the page
#6 id:60195 Posted Jan 19, 2012, 5:33 pm

I'm not sure I understand what problem this solves.

It seems like you just don't like how js looks and want something simpler.

Why not just use something simpler?

If yer just futzing around for fun, then go nuts, of course.

-Crat
http://lpmuds.net/derive.jpg
.........................
http://dead-souls.net/batsig.jpg

Runter
Wizard






Group: Developer
Posts: 3,413
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#7 id:60199 Posted Jan 19, 2012, 7:14 pm

Coffeescript is a fine language.  Compiling to js is the best strategy to solve this problem if you actually want users to be able to access your software without installing browser plugins.  The compiling process actually streamlines the code, can add multiple files and modules together, can minify the code, and can ensure safety javascript doesn't demand.  Like that lines of code are always compiled with ; at the end.  Incidentally, this is a notorious source of difficult to find problems in javascript.  The bottom line is there's compelling reason to use something like coffeescript. 

And even much more so something like cscc, which compiles to css.  But that's another story.

Last edited Jan 19, 2012, 7:14 pm by Runter
David Haley
Wizard






Group: Members
Posts: 7,841
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#8 id:60210 Posted Jan 20, 2012, 12:57 pm

There are tons of extremely reasons for simplifying syntax if your needs are simpler than what the syntax addresses. "Saving a few keystrokes" means more than saving a little time here or there. For example, it's that much less noise to keep in your head and you can focus on what you mean, not the ceremony and incantations around saying what you mean. But even if it meant just saving time, if my programs are 50% shorter, that's pretty considerable time savings.

No comment regarding this particular case because it's unclear what's motivating it, but I strongly reject the idea that saving on syntax is silly -- even when you lose flexibility. (Smart syntax will let you express simple things simply, and give you a way to say more complex things.)
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Rarva.Riendf
Wizard




Group: Members
Posts: 1,302
Joined: Jul 15, 2010

Go to the bottom of the page Go to the top of the page
#9 id:60213 Posted Jan 20, 2012, 4:14 pm

If saving a few keystroke meant anything variable names would all be 1 or 2 caracter long....Saving on syntax for the sake of it is silly

C/Java whatever :
if (toto and tata or titi) {
return "end";
}
is definitely more readable than the Lua version where you saved a few keystroke..

if toto and tata or titi
return "end"
end

Chris Bailey
Wizard




Group: Members
Posts: 860
Joined: Sep 12, 2008

Go to the bottom of the page Go to the top of the page
#10 id:60215 Posted Jan 20, 2012, 4:32 pm

That might be the most subjective thing I have seen from you.  Not only is it a stretch to say that saving on syntax is silly, it is nuts to say that your first example is definitely more readable than the second. Surely you have them backward?
   
Rarva.Riendf said:
If saving a few keystroke meant anything variable names would all be 1 or 2 caracter long....Saving on syntax for the sake of it is silly

C/Java whatever :
if (toto and tata or titi) {
return "end";
}
is definitely more readable than the Lua version where you saved a few keystroke..

if toto and tata or titi
return "end"
end


EDIT: PS: Variable names are meant to be expressive.
.........................
If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.

Last edited Jan 20, 2012, 4:34 pm by Chris Bailey
David Haley
Wizard






Group: Members
Posts: 7,841
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#11 id:60217 Posted Jan 20, 2012, 6:04 pm

Rarva, you have reduced the point to such an absurdity that of course it sounds silly. Do you want to come back to the actual point now? :smile:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Runter
Wizard






Group: Developer
Posts: 3,413
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#12 id:60219 Posted Jan 20, 2012, 7:54 pm

Code (coffeescript):
race = (winner, runners...) ->
  print winner, runners


Code (javascript):
race = function() {
  var runners, winner;
  winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  return print(winner, runners);
};


Clearly, the javascript is more readable, and saving key strokes is for newbs.

Last edited Jan 20, 2012, 7:56 pm by Runter
Chris Bailey
Wizard




Group: Members
Posts: 860
Joined: Sep 12, 2008

Go to the bottom of the page Go to the top of the page
#13 id:60221 Posted Jan 21, 2012, 12:52 pm

Rarva has broken away from the pack and taken a commanding lead in the race to crippling carpal tunnel syndrome.
.........................
If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.

Twisol
Sorcerer






Group: Members
Posts: 322
Joined: Nov 30, 2009

Go to the bottom of the page Go to the top of the page
#14 id:60223 Posted Jan 21, 2012, 6:44 pm

JohnnyStarr said:
The only way to include the new language scripts that I can see, would be to use a div with an id that has a style set
by calling the language library.

Code (HTML):
<script type="text/javascript" src="new_language.js" />
// set's the style of new_language to: 'display: none;'

<div id="new_language">
alert doc.thing
</div>


Then, on load you could execute the script:

Code (HTML):
<body onload="new_lang_execute()">

Which looks for the id's innerHTML and does the rest for you.


You can completely avoid the hackery with invisible divs by putting code for your new language in a <script> tag with a different type attribute.
Code (HTML):
<script type="text/javascript" src="/path/to/newlang.js">

<script type="application/x-newlang">
  // code using your new language here
</script>

This is the same technique CoffeeScript supports.
.........................
http://jonathan.com/wp-content/uploads/2008/09/twisolsig.png
Cratylus@Dead_Souls_Dev: beware a man who exits the bathroom smiling

Deimos
Sorcerer






Group: Members
Posts: 403
Joined: Mar 8, 2010

Go to the bottom of the page Go to the top of the page
#15 id:60224 Posted Jan 21, 2012, 7:10 pm

@David:  Of course if all he needs is to juggle element values then "limiting" the syntax to what he is suggesting makes sense, but I don't buy that that is the entirety of the use case.  And even if it's not, are you really in disagreement that creating an all new language just so you can use "obj.thing" syntax instead of "obj.thing.value" is bordering on the absurd?  Maybe I was being too kind when I suggested that he'd be losing a little flexibility - more like all flexibility :-p
.........................
Any sufficiently advanced magic will run circles around technology.

Pages:<< prev 1, 2 next >>