0

Let's say we have a simple constructor;

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

I'd rather use as follows.

function Person(firstName, lastName);

//or

class Person(firstName, lastName);

I looked through the documentation but I couldn't find anything. Is there any syntactic sugar or something like this for native javascript except its frameworks?

gurkan
  • 894
  • 3
  • 10
  • 21
  • 1
    Does this answer your question? [javascript: what's the difference between a function and a class](https://stackoverflow.com/questions/11970141/javascript-whats-the-difference-between-a-function-and-a-class) – George Aug 25 '20 at 12:56
  • https://stackoverflow.com/questions/11970141/javascript-whats-the-difference-between-a-function-and-a-class – George Aug 25 '20 at 12:56
  • I'm having trouble seeing what you're asking here...? `class` is not just something provided by frameworks, no, it's part of JavaScript (as of the 2015 spec). I think you're probably asking the question [answered here](https://stackoverflow.com/questions/11970141/javascript-whats-the-difference-between-a-function-and-a-class) (in particular, see [this answer](https://stackoverflow.com/a/45759444/157247)), but if not please clarify and ping me. – T.J. Crowder Aug 25 '20 at 12:59
  • 1
    are you asking if there is a short-hand syntax for creating classes? – Nick Parsons Aug 25 '20 at 13:01
  • 4
    I think OP is looking for a way to shorten the constructor code. For example, something like [Parameter Properties in TypeScript](https://www.typescriptlang.org/docs/handbook/classes.html#parameter-properties). @gurkan Is TypeScript an option? – str Aug 25 '20 at 13:02
  • @str - I think you're on to something there. – T.J. Crowder Aug 25 '20 at 13:04
  • @T.J.Crowder If you want the truth, to assign for each property is tiring for me :) I want to use constructor with only parameter – gurkan Aug 25 '20 at 13:23
  • @str No I especially added as **native** – gurkan Aug 25 '20 at 13:24
  • @gurkan - In that case, the first part of str's answer below is your answer I'm afraid. There isn't any shorthand in JavaScript itself for this yet. (I say "yet" because `class` syntax is being continually improved and the repetitiveness you're wanting to avoid has come up more than once.) – T.J. Crowder Aug 25 '20 at 13:32
  • @T.J.Crowder Thanks. I searched in ECMA documents but I couldn't find anything. So, can you share a post, topic, discussion, proposal or anything about it if you remember the link? – gurkan Aug 25 '20 at 13:50
  • 2
    @gurkan - I'm afraid I don't recall where I saw it. It wasn't a proposal or anything, just a passing comment. One thing I'm fairly sure of is that if it ever happens, it won't follow the TypeScript path using `public` and `private` keywords on the parameter declarations. :-) If it ever happens, it'll be at least a couple of years in the making. – T.J. Crowder Aug 25 '20 at 13:55
  • There's no syntactic sugar for this, but you can write a [function](https://jsfiddle.net/c7x39hqk/) to make the job for you. Note that it's just an example, you'll definitely need a better algorithm to extract the argument names. – Teemu Aug 25 '20 at 14:52
  • You can abbreviate a bit with `Object.assign(this, {firstName, lastName})` but there's no way to put it in the parameter declaration. – Bergi Aug 25 '20 at 14:58
  • 1
    @str Thanks for finding these duplicate topics! – Bergi Aug 25 '20 at 14:59

1 Answers1

3

You've asked if there's anything native in JavaScript itself that does that. The answer is no, JavaScript does not currently support that. You have to write the property assignments yourself (either the way you have in your first code block, or in a constructor definition in a class).


Looking beyond JavaScript itself to similar languages that can compile to JavaScript, TypeScript has Parameter properties as follows:

class Octopus {
  constructor(readonly name: string) {}
}

let dad = new Octopus("Man with the 8 strong legs");
dad.name;

Notice how we [...] just use the shortened readonly name: string parameter on the constructor to create and initialize the name member. We’ve consolidated the declarations and assignment into one location.

Parameter properties are declared by prefixing a constructor parameter with an accessibility modifier or readonly, or both. Using private for a parameter property declares and initializes a private member; likewise, the same is done for public, protected, and readonly.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
str
  • 33,096
  • 11
  • 88
  • 115