DrStrangeLove
DrStrangeLove

Reputation: 11557

object literal undefined

Q.js file

Q = {};

Q.stringFile = [];
Q.file = "CSS.txt";

Q.getData = function(Q.file){
    $.get(Q.file, function(data){
        var str = data;
        Q.stringFile = str.split("\n"); 

        return Q.stringFile;
    });
}

a.html file

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>   
    <script type="text/javascript" src="Q.js"></script>
    <script type="text/javascript">
        var d = Q.getData(Q.file);
        alert(d);
     </script>

</head>

<body>

</body>
</html>

alert doesn't output!

Errors: Q is not defined ; unexpected token .

How do i fix this??

Upvotes: -1

Views: 1417

Answers (5)

Useless Code
Useless Code

Reputation: 12402

You can't use an object property as function parameter: Q.getData = function(Q.file){. In this case there is no need for a parameter at all because of closure you can read Q.file from inside the getData function.

The reason that your alert is returning undefined is that you are making a asynchronous AJAX call it doesn't return anything so d is undefined. If you want to return something from an AJAX call you need to make a synchronous call.

A better solution however would be to use a success handler:

var Q = {}; // Without the var it is an implied global.  In this case you look like you want a global, but it's still good form to explicitly define it in the global namespace.

Q.stringFile = [];
Q.file = "CSS.txt";

Q.getData = function() {
    $.get(Q.file, function(data) {
        var str = data;
        Q.stringFile = str.split("\n"); 

        return Q.stringFile;
    }).success(function (d) { // This fires once the data has arrived
        alert(d);
    });
}; // you should have a ; here

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816272

As I said in my comment, you cannot return data from an Ajax call, as the Ajax call is asynchronous. You have to make your function accept a callback, like:

Q.getData = function(file, callback){
    $.get(file, function(data){
        var stringFile = data.split("\n"); 
        callback(stringFile);
    });
};

and then call it with:

Q.getData(Q.file, function(d) {
    alert(d);
});

Regarding the errors: You have a syntax error in this line

Q.getData = function(Q.file)

Q.file is not valid here. The browser cannot parse and process the file and so Q will not be defined.


I have the impression, you should first read some tutorial before you go on.

Upvotes: 2

RobG
RobG

Reputation: 147353

Your problem is at:

Q.getData = function(Q.file) {

The part after function( is a formal parameter list and can only contain valid identifiers. They can't contain '.' characters.

Upvotes: 2

Alex K.
Alex K.

Reputation: 175748

Your:

Q.getData = function(Q.file) {

Is not valid, that's where you define named arguments not where you pass them.

Upvotes: 1

aorcsik
aorcsik

Reputation: 15552

I think the problem is with the method definition:

Q.getData = function() {
    var that = this;
    $.get(that.file, function(data){
        var str = data;
        that.stringFile = str.split("\n"); 

        // below return has no purpose in an async request
        // return Q.stringFile;

        alert(that.stringFile);
    });
}

After running Q.getData(); your Q.stringFile will contain your data;

Also because this function does not return any value, you have to put your alert in the callback.

Upvotes: 1

Related Questions