Mohammad Hossein Amri
Mohammad Hossein Amri

Reputation: 1985

Printing a value inside and outside of a JS class

I wrote a class for practicing, can anyone tell me why console.log() prints 2 instead of 1?

function hnclass(){
    var h=1;
    function print(){
        console.info(h); //here it print 1
    }
    return {
        item:h,
        printout:print
    }
}

hc=new hnclass();
hc.item=2;
hc.printout();
console.log(hc.item); //here it print 2 

With hc.item=2; I change the value of item, so when I call the print function, it should print out 2. Is it possible to set a value without a setter function in this kind of class?

Upvotes: 1

Views: 5087

Answers (3)

Felix Kling
Felix Kling

Reputation: 816262

can anyone tell me why it print 2 instead of 1

Because setting the value of a variable or property never changes the value of another variable or property. Here is a simplified example:

var a = 1;
var b = a;
a = 42;

Assigning a new value to a doesn't change the value of b, even though b had the same value as a originally.
In your case, assigning a value to hc.item simply doesn't change the value of h.

i was thinking is it possible without a setter function set value in this kind of class?

Yes, if you also access x.item inside print:

function hnclass(){
    var h=1;
    function print(){
        console.info(obj.item);
    }
    var obj = {
        item:h,
        printout:print
    };
    return obj;
}

However, this whole setup doesn't really have anything to do with OOP or classes. Maybe Introduction to Object-Oriented JavaScript helps you to get a better understanding of the problem.

Upvotes: 2

Thinking Sites
Thinking Sites

Reputation: 3542

You didn't write a class, you wrote a function. Here are you mistakes, explicitly.

  1. You didn't scope your variables to the class by using this and instead they are private variables.
  2. You added a return statement that overrode your class definition. If you add

    console.info(hc instanceof hnclass);

you'll see that your variable hc is NOT an instance of your class.

Here is the corrected code.

function hnclass () {
    var self = this;
    this.item = 1; 
    this.printout = function print () {
        console.info(this.item); 
    }
}
var hc = new hnclass();
hc.printout(); // will display 1
hc.item = 4;

console.info(hc  instanceof hnclass); // will display true
hc.printout(); // will display 4
console.log(hc.item); // will display 4

I suggest you study more on closures, scoping, how functions differ from classes, and how to test if a variable belongs to a class with instanceof.

Upvotes: 1

Holt
Holt

Reputation: 37606

In your 'class' declaration, the h is a local variable. If you want to create a attribute, you have to do something like :

function hnclass () {
    this.item = 1; // Here item is a attribute of your class
    this.printout = function print () {
        console.info(this.item); // Use this.item instead of h
    }
}
hn = new hnclass () ;
hn.printout() ;
hn.item = 4 ;
hn.printout() ;

This is a very simple example, you can use prototype to have a more organized code.

Upvotes: 0

Related Questions