gilbert / es-explicit-this

Explicit this naming in ECMAScript functions
18 stars 2 forks source link

Another alternate syntax :D #2

Open xooxdoo opened 8 years ago

xooxdoo commented 8 years ago
function ::zip (array, otherArray) {
  return array.map( (a, i) => [a, otherArray[i]] )
}

function ::flatten (subject) {
  return subject.reduce( (a,b) => a.concat(b) )
}

[10,20]
  ::zip( [1,2] )
  ::flatten()
//=> [10, 1, 20, 2]

// Flow/TypeScript/SoundScript
function ::zip<T>(array: Array<T>, otherArray: Array<T>): Array<T> {
  return array.map( (a, i) => [a, otherArray[i]] )
}

function ::flatten<T>(subject: Array<T>): T {
  return subject.reduce( (a,b) => a.concat(b) );
}

As you can see this solves the too many colones you mentioned in #1. And keep the aspects of what i was talking about transparency between the two specs :D.

Also i couldn't grasp the fact that in the first syntax ...

function method (this elem, e) {
//               ^-- `this` is an argument; I simply renamed it therefor I can use it
  console.log("Elem:", elem) // OK
  console.log("this:", this) // <-- Throws an error!
//                       ^-- ISSUE: `this` is allowed in the signature and not the body

It seems logic to me that if you are not going to let me use this keyword inside the function, you should ask me to use it in the argument either :D.

Wish the best for you and your suggestion, but please take my syntax in consideration.

And as I suggested in #1 consider merging it with the other spec if that is possible.

hax commented 8 years ago

Here is another alternative from TypeScript community (https://github.com/Microsoft/TypeScript/issues/6018#issuecomment-164757557)

function filter<T> Iterable<T>::(callback: (value: T) => boolean): Iterable<T> { ... }

Eliminate the type annotations and add thisArg:

function filter thisArg::(callback) { ... }
function zip array::(otherArray) {
  return array.map( (a, i) => [a, otherArray[i]] )
}

function flatten subject::() {
  return subject.reduce( (a,b) => a.concat(b) )
}

I don't much like this syntax, just FYI.

xooxdoo commented 8 years ago

Also I used C# more the past few months and this is very helpful to add functionalities to third party objects without extending them, a really like it now.

they should really add this to JS