Skip to content

Instantly share code, notes, and snippets.

@valueof
Created May 30, 2012 23:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save valueof/9a6d60e9c15ac4239b3d to your computer and use it in GitHub Desktop.
Save valueof/9a6d60e9c15ac4239b3d to your computer and use it in GitHub Desktop.
A piece of ordinary boilerplate JavaScript code (original.js) rewritten using proposed moustache syntax (moustache.fixed.js) and class syntax as implemented in Traceur (moustache.class.js)
class Channel {
constructor (opts) {
/* ... */
}
load () {
var widget = document.createElement("iframe");
widget.{
style.{
width = "100%";
border = "none";
overflow = "hidden";
display = "none";
};
dataset.{
disqusUid = this.uid;
};
allowTransparency = true; // < IE8 iframe transparency
frameBorder = 0;
width = "100%";
id = "dsq" + this.uid;
src = this.target + "#" + this.uid;
};
}
};
var Channel = function (opts) { /* ... */ };
Channel.prototype.load = function () {
var widget = document.createElement("iframe");
widget.{
style.{
width = "100%";
border = "none";
overflow = "hidden";
display = "none";
};
dataset.{
disqusUid = this.uid;
};
allowTransparency = true; // < IE8 iframe transparency
frameBorder = 0;
width = "100%";
id = "dsq" + this.uid;
src = this.target + "#" + this.uid;
};
};
var Channel = function (opts) { /* ... */ };
Channel.prototype.load = function () {
var widget = document.createElement("iframe");
widget.{
style.{
width = "100%";
border = "none";
overflow = "hidden";
display = "none";
};
allowTransparency = true; // < IE8 iframe transparency
frameBorder = 0;
width = "100%";
id = "dsq" + this.uid;
data-disqus-uid = this.uid
src = this.target + "#" + this.uid;
};
};
var Channel = function (opts) { /* ... */ };
Channel.prototype.load = function () {
var widget = document.createElement('iframe');
widget.setAttribute('style', 'width:100%; border:none; overflow:hidden; display:none');
widget.setAttribute('allowTransparency', 'true'); // < IE8 iframe transparency
widget.setAttribute('frameBorder', '0');
widget.setAttribute('width', '100%');
widget.setAttribute('id', 'dsq' + this.uid);
widget.setAttribute('data-disqus-uid', this.uid);
widget.setAttribute('src', this.target + '#' + this.uid);
// ...
};
@mikeal
Copy link

mikeal commented May 31, 2012

just want to float this, what is wrong with obj.update({ foo: bar }) or Object.update(obj, {foo: bar})?

this achieves the desired flexibility in nesting without new syntax and is definitely explicitly "updating" the object.

@isaacs
Copy link

isaacs commented May 31, 2012

Are there not enough bikesheds?

@valueof
Copy link
Author

valueof commented May 31, 2012

@ljharb:

So what happens if you do foo.{ bar = true; } when bar isn't a previously defined property on foo?

Same as foo.bar = true; today.

@mikeal:

just want to float this, what is wrong with obj.update({ foo: bar }) or Object.update(obj, {foo: bar})?

I feel like it's similar to your argument against o.{ a = b }. To figure out whether you are defining a new property or updating the current one you will have to check the code to see if it's o.update({ ... }); or o.otherMethod({ ... }); or o = { ... };.

@dherman
Copy link

dherman commented May 31, 2012

@mikeal: I mostly agree; I don't think "mustache" has been all that well motivated so far. I've been opposed to it for the most part since the beginning, but for me where I started to see some more win was when -- with the variation using assignment syntax -- I realized it could also be used for chaining (aka the fluent style aka cascades) for API's that don't return this for you. For example:

a.{ pop(); pop() }

or

a{ .pop(); .pop() }

or what have you. But I generally agree with you that syntax has to be well-understood and well-motivated to be worth adding. Mustache is definitely not at that point.

Dave

@mikeal
Copy link

mikeal commented May 31, 2012

@antonkovalyov

those rules are only as unclear with .update as they are with new syntax. you still have to learn the rules, the only difference is that there is an english word pointing you in the right direction.

@dherman

i agree, it's not compelling enough to me yet to justify the loss in assumptions. while i agree that using : is not clear about updating it doesn't fall in to the trap of stepping on the existing assignment semantics because you already have to look to the top of an object literal definition to understand it.

@valueof
Copy link
Author

valueof commented May 31, 2012

@mikeal, yeah I agree—was just pointing out that it doesn't solve the unclear rules problem.

@mikeal
Copy link

mikeal commented May 31, 2012

sure, not entirely, but it doesn't step on the rules people now assume about assignment statements that exist entirely outside of using this feature.

@BrendanEich
Copy link

Object.update, w00t.

We killed <| the other week. I think .{ is going down. But we should really do something like Object.update (Prototype's Object.extend). We talk about paving cowpaths, that's an obvious one and it leads somewhere good.

/be

@valueof
Copy link
Author

valueof commented Jun 1, 2012

@BrendanEich, yes, Object.extend (update) was one of the most popular items on JSFixed.

@dherman
Copy link

dherman commented Jun 1, 2012

@isaacs Here's a lovely unpainted bikeshed for you: how about Object.update for deep and Object.set for shallow?

Dave

@isaacs
Copy link

isaacs commented Jun 1, 2012

Given the proliferation of subtly different Object.extend implementations, I think this would be a good thing for the ES spec to lay down a decision on. In node, we eventually just removed util.extend entirely, because it was too complicated to satisfy everyone's desires for it to be all things, handling getters and deeply nested objects and so on. New syntax for this is silly, though.

@dherman If I understand you properly, then, Object.update would deep-copy, and Object.set would shallow-copy?

So:

var a = { b: { c: 'd', e: Object.create(Object.prototype, {f: { get: function () { throw 'no can get!' }, configurable: false }}) } }
var b = Object.update({x: 'y'}, a)
var c = Object.set({x: 'y'}, a)

In this example, then, c.b === a.b, b.b !== a.b, and b.b.e is an evil getter?

I have no strong opinions about the method names. set and update are fine. But it is a bad idea to introduce new syntax for this, even though it is cute.

@BrendanEich
Copy link

@izs: agree on syntax being bad (premature, unjustified, impossible to polyfill, etc.).

@dherman: first, I wonder if we shouldn't just try to standardize Object.extend as the shallow thing, and make it match prototype but handle enumerable accessors too.

Second thought: naming. The set vs. update names do not connote shallow vs. deep. No name that I know of does, so the boring but clear way is Object.deepExtend. Too long? It's not the common case going by existing libraries I know of.

/be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment