1

I am defining an object literal in JavaScript, but when I try to create a property that is a called function, I get a Uncaught ReferenceError: xxx is not defined error.

Here's an example:

var person = {
    name : 'Saucy Jack',

    convenienceHook : this.getName('some predefined string'),

    getName : function(string) {
        console.log(this.name + string);
    }
};

This errors saying Uncaught ReferenceError: convenienceHook is not defined.

Nor does it work if I assign the property outside the object definition:

var person = {
    name : 'Saucy Jack',

    getName : function(string) {
        console.log(this.name + string);
    }
};

person.convenienceHook = person.getName('some predefined string');
person.convenienceHook(); //returns error 

What am I missing?

4
  • In the first instance getName() isn't defined by the time you call it as the object instantiation isn't complete. In the second instance, getName() doesn't return anything (or returns undefined) which isn't a function. Commented Jul 31, 2015 at 21:23
  • That's not the error I'm getting (TypeError: this.getName is not a function) Commented Jul 31, 2015 at 21:23
  • what is convenienceHook supposed to be, a pointer to get name function with a parameter Commented Jul 31, 2015 at 21:24
  • getName doen't return anything ??? fubar Commented Aug 1, 2015 at 18:53

4 Answers 4

4

Here:

var person = {
    name : 'Saucy Jack',

    convenienceHook : getname('some predefined string'),

    getName : function(string) {
        console.log(this.name + string);
    }
};

you’re calling a function called getname that doesn’t exist. getName is not a property on person yet, because getname('some predefined string') has to be evaluated before the object can exist, since it’s part of the object literal. Even if it did exist, though, you still wouldn’t be able to call it as a bare getname, and even if it did, you would have to capitalize getName properly.

Your second approach also just calls person.getName. It looks like you want to make another function that will call getName:

var person = {
    name : 'Saucy Jack',

    convenienceHook : function() {
        return this.getName('some predefined string');
    },

    getName : function(string) {
        console.log(this.name + string);
    }
};
Sign up to request clarification or add additional context in comments.

5 Comments

your code doesn't match OP. convenienceHook : this.getName not convenienceHook : getname. And the reason this fails is not that getName isn't a property of person yet, it's that it isn't a property of this (global scope)
Sorry - I posted with a few typos. I edited the question. Are you saying there's not a way to have an evaluated function as a property inside an object literal definition if that function is a method of the object being created?
@GloryOfThe80s: this refers to the same thing inside an object literal as it does outside. It’s only inside functions that this can change. The explanation about person not existing still applies, anyway – this cannot ever refer to person at that point, because person has not been assigned a value and the value that will be assigned to it does not yet exist.
@GloryOfThe80s: Right, you can’t use a property of an object you’re creating before that object exists, i.e. while you’re creating it.
@minitech - I didn't see the "before" version (it's not in the edit history either, I guess it was quickly corrected)
0

What you assigning is not a function, you need for example to return a function from the getName function, so the convenienceHook will be a function:

getName : function() {
    return function() {
        console.log(this.name);
    };
}

Comments

0

The reason your first attempt fails is that this is not person, it's whatever this is in your current scope. could be global, or could be something else if that code is within a function.

Comments

0

This should work

var person = {
name: "Saucy Jack", 
convenienceHook: function() { return this.getName("Predefined text") },
getName: function(string) { 
    string=string||"";
    return this.name + string; } 
}

5 Comments

No, that doesn’t work. person doesn’t have a value when person.getName is evaluated.
That still won’t work and is more or less the same as the question itself.
@minitech In your answer, it works except if you put person.getName() it returns Saucy Jackundefined, my solution has no problem with that
Okay, seems to me that that’s not a problem though.
AND YES MY SOLUTION WORKS!! Remove wrong downvotes and mark as answer

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.