nikhilk / scriptsharp

Script# Project - a C# to JavaScript compiler, to power your HTML5 and Node.js web development.
http://scriptsharp.com
Other
658 stars 182 forks source link

Improving Script Generation #138

Open dax70 opened 12 years ago

dax70 commented 12 years ago

The following are observations/suggestions I would like to see and willing to contribute and help. These are based on build .0.7.4.0.

Extend/Merge Inheritance Pattern:

Implementation Take 2 objects and essentially merge their properties, fields and functions to get a newly merged object.

jQuery, Backbonejs, Coffescript Classes (generated code), and many more libraries use this pattern for inheritance.

Here is an example from John Resig (jQuery author), though a few others had this pattern before.


var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});
var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  }
});

Aside from the cleaner looking code, it is also more compact then the current output with lots of potential for optimization.

These are often implemented thru helper functions. Usually, both the return value and the first object are the subtype, any functions/properties in common will be overridden by the first object passed in.

// jQuery
var object = $.extend({}, object1, object2);

// Underscorejs
_.extend(destination, *sources) 

As I mentioned CoffeeScript uses this pattern for inheritance as well, with the added benefit of the super variable enabling calling methods on the base class. I think it's perhaps worth looking into and or mimicking this behavior.

Namespaces

Namespaces are usually implemented with a combination of a scoped object and the javascript module pattern.

jQuery, YUI, and many others use this same pattern, allowing for later reassignment/aliasing of the name as is the well known: $ = jQuery;

(function(){

var root = this;

var previousNamespace = root.Namespace;
// merge

var Namespace = {};

// Start adding objects.
Namespace.Foo = ...

})();

More use of Object Literals

Liberal use of object literals specially for simple object and perhaps even as a means of encapsulating parameters (more on Method overloading section).

Anonymous functions

Rather than generate a name and assign it to a public variable, simply leave the function anonymous and assigned it to the desired name of the C# method. I find this both more idiomatic to javascript, with the added benefit of having less characters.

Multicast Delegates/ .NET Events

Perhaps not a necessity at the momment, but I really like the functionality (power and terseness of the Backbonejs Events module

Internally it uses the Underscorejs Bind function. The bind function can have other useful features for the generated script. As the documentation points out, it ensure the scoping of this variable to be the object containing the function, specially in scenarios where the this variable can not be easily captured before a closure function call.

Linq

Seeing how I keep mentioning UnderscoreJs, I figure I should stick with the theme. While there are several other libraries that provide Linq like functionality and even syntax, as you can see from the docs many of the functions are similar to Linq to Objects, at least similar in intent.

Method Overloading

I'm unsure if you don't allow method overloading since this is lacking in Javascript or as a result or parser work. I have a few ideas here as well. This one is obviously a bit trickier. Some ideas rely on the helper functions mentioned such as extend, map, each, etc. For example the Events module could be repurposed to bind to different functions and then map the appropriate parameters.

This post is getting a bit long, so I'll reserve this for another time.

Looking forward to hearing what your thoughts are on some of these.

Cheers,

Eddy

andrewharry commented 12 years ago

Top of my wish list.... Support for Generics

jonsamwell commented 11 years ago

Any movement on supporting Linq operations?

theoutlander commented 11 years ago

Hopefully soon! Perhaps sooner if we get more community support :)