proposal-class-public-fields icon indicating copy to clipboard operation
proposal-class-public-fields copied to clipboard

Examples?

Open rwaldron opened this issue 9 years ago • 6 comments

Where can I find examples (with explanations) of the syntax in its present state? That seems like something that belongs in this proposal repository eg.

  • https://github.com/tc39/ecmascript-asyncawait/blob/master/server.asyncawait.js
  • https://github.com/rwaldron/exponentiation-operator#usage
  • https://tc39.github.io/proposal-template-literal-revision/
  • https://github.com/wycats/javascript-decorators/blob/master/README.md

Thanks!

rwaldron avatar Aug 10 '16 19:08 rwaldron

I've just implemented most of this feature in V8 as an experiment, and am about to write tests from which I can extract some examples to post here.

bakkot avatar Aug 10 '16 22:08 bakkot

These are just to help anyone coming across this before we get more complete examples in the repo.

Basic example:


class Counter {
  count = 0;

  increment() {
    ++this.count;
  }
}

let counter = new Counter;
counter.increment();
console.log(counter.count); // 1

Lots of syntax examples:

// Basic syntax

class C {
  a = 0;
}

class C {
  a;
}

class C {
  static a = 0;
}

class C {
  static a;
}


// ASI examples

class C {
  a = 0
  b(){}
}

class C {
  a
  b
}

class C {
  a
  *b(){}
}

class C {
  a
  ['b'](){}
}

class C { // a property named 'a' and a property named 'get'
  a
  get
}

class C { // a single static property named 'a'
  static
  a
}

class C { // a property named 'a' and a property named 'static'
  a
  static
}


// ASI non-examples / errors

class C { a b } // There is no line terminator after 'a', so ASI does not occur

class C { // '*' may follow 0 in an AssignmentExpression, so no ASI occurs after 0
  a = 0
  *b(){}
}

class C { // '[' may follow 0 in an AssignmentExpression, so no ASI occurs after 0
  a = 0
  ['b'](){}
}

class C { // 'a' may follow 'get' in a MethodDefinition, so no ASI occurs after 'get'
  get
  a
}


// Non-examples

class C { // a getter for 'a' installed on C.prototype; this is existing syntax
  get
  a(){}
}

bakkot avatar Aug 10 '16 23:08 bakkot

@bakkot

-class C { // 'a' may follow 0 in a MethodDefinition, so no ASI occurs after 'get'
+class C { // 'a' may follow 'get' in a MethodDefinition, so no ASI occurs after 'get'

michaelficarra avatar Aug 11 '16 05:08 michaelficarra

Is there any way to get arguments of constructor?

From

class A {
  constructor (opts) {
    this.someProp = opts.someProp
  }
}

let a = new A({someProp: 'Some value'})

To

class A {
  someProp = ???(opts.someProp)
}

let a = new A({someProp: 'Some value'})

Rokt33r avatar Dec 07 '16 13:12 Rokt33r

@Rokt33r, not at the moment; see #33 for that general discussion and also https://github.com/sebmarkbage/ecmascript-scoped-constructor-arguments for a proposed fix.

Personally I think that doing the initialization in the constructor is fine, though; that's common enough in other languages, and is more consistent than making constructor arguments available outside their scope.

bakkot avatar Dec 07 '16 17:12 bakkot

@bakkot Thanks for the super helpful answer! 💯

Rokt33r avatar Dec 07 '16 18:12 Rokt33r