2

So I was playing around in typescriptlang.org/play, writing a class of Plane with a method of pilot(): void {} and I pasted the JS code inside my Chrome console and played around with that for a minute.

Then I wanted to put into practice the concept of being able to add another method to class Plane {}. So this is what I had on the TypeScript side:

class Plane {
    color: string = 'red’;

    pilot(): void {
        console.log(‘swoosh’);
    }

    takeoff(): void {
        console.log(‘ready for takeoff’);
    }
}

This is JS version:

class Plane {
    constructor() {
      this.color = 'red';
    }

    pilot() {
        console.log(‘swoosh’);
    }

    takeoff() {
        console.log(‘ready for takeoff’);
    }
}

When I pasted that into Chrome console I got Uncaught SyntaxError: Identifier 'Plane' has already been declared.

Okay, so how do I add a new method then? I should be able to easily attach as many methods to prototype as I want. Is it because the term prototype does not exist in my JS code?

1
  • Similar question has been answered here Commented May 27, 2020 at 15:53

2 Answers 2

1

class functions like const or let in JS-land: it can't be re-declared. When I'm pasting code in the console that uses those terms, I generally refresh the page each time.

But happily, new releases of Chrome are letting you re-declare let and const within the console. I don't know if this will ever extend to class.

Note that you can, indeed, add a line like Plane.prototype.foo = function() {} after Plane has been declared, and this will work as you'd expect.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response, because the JS version of the TypeScript is not offering the use of prototype like a did some months ago, I now actually have to attach it to the class object and then attach the new method to it. Nevermind, it looks like you added this to your answer as well.
0

It seems like you are pasting the JavaScript code into a console that already has your Plane class defined.

If you already pasted the class definition once you cannot redeclare it by pasting an edited version again, you need to refresh your page or just open a console in an empty tab to get a fresh environment.

If though you want to experiment with adding a method to an existing class you can do so like this:

// You start by defining the original class (or picking one of the defined ones like Date)
class Plane {
  // Your class definition
  getName() { return 'I am a plane'; }
}

// Then somewhere later in your code (or console)
Plane.prototype.sayHello = function() {
  return 'Hello ' + this.getName();
}

// And finally
const plane = new Plane();
plane.sayHello(); // Will return Hello I am a plane

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.