5

As the example below, how to avoid write "this" everywhere, is there a way to write heroes, myHero,lastone without "this" ? Just like common javascript

enter image description here

4
  • try this=xyz then use xyz instead of this Commented Feb 17, 2016 at 7:16
  • It is possible (with certain shenanigans) but it doesn't make sense. So stick to traditional notation. Commented Feb 17, 2016 at 7:25
  • 1
    @PardeepJain replace this to xyz is meaningless, I just want to write variable directly Commented Feb 17, 2016 at 7:38
  • Possible duplicate of: stackoverflow.com/questions/34894886/… Commented Feb 17, 2016 at 9:43

2 Answers 2

3

When you use a class you are dealing with properties and methods, not variables and functions. That's why this is required - it refers to the instance of a class.

You can still use local variables and functions inside class methods, but if you need to access class (instance) property or call it's methods you have to use this.

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

Comments

-1

You can declare your variable out of your class:

var heroes = {...}
class AppC {
    test() {
        //heroes 
    }
}

I DO NOT advise to do that.

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.