Yunhan Li
Yunhan Li

Reputation: 153

Passing value to another prototype function

Getting into the Javascript prototype, I have no idea why when I call one prototype function from another and pass value to another, the value does not get updated.

Is that some problem related to closure? I tried to use global variable but still doesn't work. Any assistance?

function test(elem){
   this.opt = 
   this.elem = $(elem)
   this.method1();
}

test.prototype.method1 = function() {
   var output = 1;
   this.method2(output);
   console.log(output);
}

test.prototype.method2 = function(output) {
   output += 1;
}

var data = new test(this);

When I call method2 in method1 function, the output will not get updated, it will still console 1 as a result.

Upvotes: 2

Views: 244

Answers (3)

Cristi Pufu
Cristi Pufu

Reputation: 9095

Your problem is basically reference vs value

Javascript is always pass by value, but when a variable refers to an object (including arrays), the "value" is a reference to the object.

Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object.

However, changing a property of an object referenced by a variable does change the underlying object.

You have 3 posibilities:

  1. Wrap the variable in an object: http://jsfiddle.net/8c2p349g/

function test(elem, opt){
       this.opt = opt;
       this.elem = $(elem);
       this.method1();
    }
    
    test.prototype.method1 = function() {
        var data = {
            output: 1
        };
       this.method2(data);
       console.log(data.output);
    }
    
    test.prototype.method2 = function(data) {
       data.output += 1;
    }
    
    var inst = new test();

  1. Return output from method2: http://jsfiddle.net/8c2p349g/1/

function test(elem, opt){
       this.opt = opt;
       this.elem = $(elem);
       this.method1();
    }
    
    test.prototype.method1 = function() {
       var output = 1;
       output = this.method2(output);
       console.log(output);
    }
    
    test.prototype.method2 = function(output) {
       return output + 1;
    }
    
    var inst = new test();

  1. Attach output as property of test: http://jsfiddle.net/8c2p349g/2/

function test(elem, opt){
       this.opt = opt;
       this.elem = $(elem);
       this.method1();
    }
    
    test.prototype.method1 = function() {
       this.output = 1;
       this.method2(this.output);
       console.log(this.output);
    }
    
    test.prototype.method2 = function(output) {
       this.output += 1;
    }
    
    var inst = new test();

Upvotes: 2

Cerbrus
Cerbrus

Reputation: 72857

In method2, output is a variable on that function's scope.
It doesn't point to the output in method1.

You're going to have to return the new value from method2:

test.prototype.method1 = function() {
    var output = 1;
    output = this.method2(output);
    console.log(output);
}

test.prototype.method2 = function(output) {
   return output + 1;
}

Upvotes: 1

meskobalazs
meskobalazs

Reputation: 16041

The output is a local variable of method1, it does not exist in the scope of method2.

Upvotes: 0

Related Questions