3
class Auto {
  printauto() {
    var type = "2 wheeler";
    var color = "green";
    console.log("Car type is="+this.type+" "+" and color="+this.color);
 }
}

var a = new Auto();
a.printauto();

why is output undefined?

Car type is=undefined and color=undefined

2
  • 2
    Because a variable is not the same as a property. var type and this.type are two different things. Commented Apr 28, 2017 at 18:41
  • 1
    this.type is undefined because you never set this.type to anything. Commented Apr 28, 2017 at 18:41

3 Answers 3

1

Attaching a variable to the class context doesn't happen just by declaring it. You have to be explicit:

class Auto {
  printauto() {
    this.type = "2 wheeler"; // explicitly attach to `this`
    this.color = "green";    // same here
    console.log("Car type is="+this.type+" "+" and color="+this.color);
 }
}

new Auto().printauto();

The constructor is usually the best place for such initialization:

class Auto {
  constructor() {
    this.type = "2 wheeler"; // explicitly attach to `this`
    this.color = "green";    // same here
  }
  printauto() {
    console.log("Car type is="+this.type+" "+" and color="+this.color);
 }
}

new Auto().printauto();

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

Comments

1

This refer to class and you don't have color and type declared in your class. The color and are scoped inside your printauto method. either you declare them in your class or just do this

console.log("Car type is="+type+" "+" and color="+color);

By declaring in class

class Auto {
    this.type = "2 wheeler";
    this.color = "green";

    printauto() {
        console.log("Car type is=" + this.type + " " + " and color=" + this.color);
    }
}
var a = new Auto();
a.printauto();

4 Comments

thank you and why i get error for below code:- class Auto { var type = "2 wheeler"; var color = "green"; printauto() { console.log("Car type is="+type+" "+" and color="+color); } } var a = new Auto(); a.printauto();
you don't use var inside your class just variable name with public or private class Auto { type = "2 wheeler"; color = "green"; printauto() { console.log("Car type is=" + this.type + " " + " and color=" + this.color); } } var a = new Auto(); a.printauto();
That syntax is still not part of the language. Properties cannot be put on classes at this time.
@Yogi i was bit wrong, i mixed it up with type script you have to use this inside your class for declare variables. See my updated answer. it will be supported to declare properties inside class without this key word.
0

As described by Felix King in comment, variable a is not the same as properties of class Auto

class Auto {
  printauto() {
    var type = "2 wheeler";
    var color = "green";
    console.log("Car type is="+ type+" "+" and color="+color);
 }
}

var a = new Auto();
a.printauto();

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.