FlyingCat
FlyingCat

Reputation: 14290

How to assign property under functions?

I am trying to assign the property under a function. I have

test.prototype.getName = function(){
    var myself=this;

     //ajax callback function
      call.callback = function(obj){

     //I want to assign returned ajax obj to test property.       
        myself.testName=obj;
      }

}


test.prototype.build = function(){

this.getName();

console.log(this.testName)//undefined...

}


test.build();

How do I get the this.testName shown in my build function? Thanks a lot!

Upvotes: 0

Views: 65

Answers (3)

freethejazz
freethejazz

Reputation: 2285

Based on the code you've given, I'm making the following assumptions:

Assumption 1:

You've initialized test to a function that returns some object or function, but haven't included that code in your question. This is called a constructor. I assume it looks something like:



    function test(){
        //do some work here
        return this;
    }

//or

    var test = function(){
        //do some work here
        return this;
    }

I assume this because you're able to set properties of the prototype without throwing errors, which the above would allow you to do.

Assumption 2:

I assume you're trying to go for some object oriented solution that involves more than one instance of your test object. This assumption is based on the fact that you are aiming to use prototypal inheritance, which is less important if your 'test' function is meant to have only one version(this is called a singleton).

Based on these assumptions, the problem is that you're not creating an instance of your test object. The following code should work fine(relies on assumption 1);



    var testInstance = new test();
    testInstance.build();
    // wait for AJAX to return and execute success handler
    testInstance.property // returns a proper value

If build is something you'll do only once and something you want to do for every instance of the test object, then you can put it into the constructor itself:



    function test(){
        this.build();
        return this;
    };
    
    
    var testInstance = new test();
    // wait for AJAX to return and execute success handler
    testInstance.property // returns a proper value

Here's a full test, using setTimeout as the asynchronous call



    function Test(){
        //do anything
        return this
    };
    Test.prototype.getName = function(){
        var self = this;
    
        //simulate AJAX with setTimeout
        setTimeout(
        function(){
            self.testName = "Ahmad Jamal";
            console.log('name set');   
        }, 1000);
    };
    Test.prototype.build = function(){
        this.getName();
        console.log(this.testName); // this is undefined, as the setTimeout is still waiting
    };
    
    var test = new Test();
    
    test.build();
    // now wait to see 'name set' in the console
    
    test.testName; // returns "Ahmad Jamal"

Upvotes: 1

Husni
Husni

Reputation: 1065

Something like this?

function MyClass () {}

function test () {}

test.prototype = new MyClass();

test.prototype.getName = function(callback){
    this.testName = callback();
};

test.prototype.build = function(){
    this.getName(function() {
        return 'this is my test name';
    });
    console.log(this.testName);
}

var myobj = new test();
myobj.build();

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102793

Given the nature of asynchronous functions, you have to use a callback function. Pass the function with console.log to getName, and execute it when the asynchronous operation is complete:

test.prototype.getName = function(callback){
    var myself=this;

    //ajax callback function
    call.callback = function(obj){

        //I want to assign returned ajax obj to test property.       
        myself.testName=obj;
        callback();
    }
}

test.prototype.build = function(){ 
    var myself = this;

    this.getName(function() {
        console.log(myself.testName)
    });
}

Upvotes: 1

Related Questions