ractivejs / ractive

Next-generation DOM manipulation
http://ractive.js.org
MIT License
5.94k stars 396 forks source link

Suggestion: simplify the syntax to instantiate a Ractive object #1698

Closed benpptung closed 9 years ago

benpptung commented 9 years ago

Hi....I am looking into Ractive.js and thank you for the great work. Just wondering if we can instantiate Ractive object in the following way?

var Ractive = require('ractive');
var ractive = Ractive({
    template: 'hello from {{who}}'
});

It is to just add one line in the beginning of Ractive constructor like the following example.

if (this instanceof Ractive !== true) return new Ractive(options);

I suggest this because I rarely use new to instantiate an object now. Use UpperCamelCase for class names is clear enough, and sometimes it can shorten the codes a lot in the following way:

var ractive = require('ractive')({ template: 'hello from {{who}}' });

I assume it won't cause any issue, and apologized if I am wrong. :)

pakastin commented 9 years ago

+1

dagnelies commented 9 years ago

-1 I consider this syntactic sugar as unnecessary. Also, there are various mechanisms to extend Ractive in various ways, for example with components. Dunno if this would be a problem or not. Lastly, with new it is clearer that you create an instance rather than invoking a function or a singleton.

benpptung commented 9 years ago

@dagnelies, I understand what you mean. I would like to explain it more in detail. In fact, it won't force you to drop new. It is just an additional way for developers who are used to node.js style coding. In node.js, it is a very common style, even in its core api, e.g. Buffer. After being familiar to it, it is really hard to write a new before a constructor.

martypdx commented 9 years ago

There certainly is some popularity in the js community with that convention, and it would be entirely elective.

While the cost of the instanceof check seems low (thousandths to hundredths of a ms), I would prefer to not to incur the cost on template instantiated components, just developer new instantiations. So I'd need to look at the code, but I think that's doable.

Biggest thing is for people who would like this to chime in. Generally one-off preferences are better as self-extensions.

Rich-Harris commented 9 years ago

I would prefer to not to incur the cost on template instantiated components, just developer new instantiations

That's actually the default - constructor functions aren't called for inline components. So AFAICT the instanceof check would just need to be added here and here, and I just did a million instanceof checks in under 3 milliseconds so I reckon we're okay on that front.

So I don't have any particular objection to this. Not that I've ever really understood the appeal of dropping new - it means an extra function call, and is less idiomatic/explicit than the alternative. (Or, to paraphrase, 'get off my lawn!')

fskreuz commented 9 years ago

I'm ok with both :D

Disclaimer: I use components extensively (peppering <custom-element /> tags all over the place) and I don't see new Ractive({}) apart from the initial component insert call. :trollface:

Rich-Harris commented 9 years ago

Have created a PR (#1715) - any strong objections to it? (@dagnelies - it's optional, and the Ractive.extend() case is covered, which hopefully addresses your concerns)

dagnelies commented 9 years ago

@Rich-Harris fine for me, it's your call! ;)