0

I'm trying to achieve a snippet corresponding to an example found in this book. Here my Javascript snippet: "use strict"

function readObject(target, name, descriptor) {
    return console.log("reading object...: ", target, name, descriptor)
}


class Example {
  a() {}
  @readObject
  b() {}
}

const e = new Example();
e.a = 1;
e.b = 2;

My console returns:

line 12 _ SyntaxError: illegal character

I have also tried:

function readonly(target, name, descriptor) {
  descriptor.writable = false;
  return descriptor;
}


class Example {
  a() {}
  @readonly
  b() {}
}

const e = new Example();
e.a = 1;
e.b = 2;

the later trial fails too.

It seems to me I have defined readObject.

Maybe I have forgot an another element?

Any hint would be great, thanks.

Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • 1
    Have you tried removing the @readObject string? This is not valid JS – Loupax Mar 26 '19 at 12:37
  • it may be because you forgot to add a semicolon – N8888 Mar 26 '19 at 12:37
  • 1
    @Loupax *cough* https://stackoverflow.com/q/43447082/476 – deceze Mar 26 '19 at 12:37
  • 1
    @Loupax see also: https://www.typescriptlang.org/docs/handbook/decorators.html – Mark Mar 26 '19 at 12:38
  • seems issue is with your decorator not being properly defined – N8888 Mar 26 '19 at 12:39
  • 1
    From the above link: *"Decorators use the form @expression, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration"* `readObject()` does not evaluate to a function. – Mark Mar 26 '19 at 12:43
  • @deceze ok, wow, I just looked away for a second and we now have decorators? Madness! – Loupax Mar 26 '19 at 12:45
  • 1
    @Loupax -- I don't think we can use decorators yet in Node, but they're in Typescript. – Mark Mar 26 '19 at 12:47
  • @Webwoman It's somewhat unclear, what you are trying to achieve. Should your decorator always be called, when you access the `b` property of your `Example` class? – yunzen Mar 26 '19 at 13:09
  • thanks for comments, I have also tried with objects following the book's example, seems it fails. I'm just trying to reproduce a book example to ensure I'm following the book pace and understanding the concept illustrated inside – Webwoman Mar 26 '19 at 13:27

2 Answers2

0

Seems it was a transpiler probem, the code works better using appropriated transpiler, in jsfiddle for example:

https://jsfiddle.net/wergsdba/1/

console.log("go on the link to appreciate the code using ES7")
Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • Please check my answer below. Could you test this out and give me some feedback? – Der Alex Mar 26 '19 at 13:47
  • yes of course, I have just tested it and yep, we can also use decorators on class properties, your code works too with ES7 – Webwoman Mar 26 '19 at 13:54
-1

I guess the real error message would be something like: "Leading decorators must be attached to a class declaration".

The decorator has to be before the class declaration, not inside.

EDIT You also can use decorators on class properties. I guess the code should look something like this:

class Example {
  @readObject
  a = {};
  constructor(){
    //...
  }
}
Der Alex
  • 718
  • 3
  • 16