Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Examples? #46

Open
rwaldron opened this issue Aug 10, 2016 · 6 comments
Open

Examples? #46

rwaldron opened this issue Aug 10, 2016 · 6 comments

Comments

@rwaldron
Copy link

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.

Thanks!

@bakkot
Copy link
Contributor

bakkot commented Aug 10, 2016

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
Copy link
Contributor

bakkot commented Aug 10, 2016

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(){}
}

@michaelficarra
Copy link
Member

@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'

@Rokt33r
Copy link

Rokt33r commented Dec 7, 2016

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'})

@bakkot
Copy link
Contributor

bakkot commented Dec 7, 2016

@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.

@Rokt33r
Copy link

Rokt33r commented Dec 7, 2016

@bakkot Thanks for the super helpful answer! 💯

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants