0

I am collaborating on a dynamically generated web page that uses mustache and js to determine what to display. I want to have a function that returns one object given a certain global condition and a different object if that condition is not the case, but I can't seem to get it to work.

getNameTokens: function() {
    var tokens;
    if(world.indexOf('9') == 1 ||world.indexOf('9') == 0) {
        tokens = {agent: 'president', ent: 'company', ownsf: 'the company\'s', owns: 'its'};
    } else {
        tokens = {agent: 'player', ent: 'player', ownsf: 'his', owns: 'his'};
    }
    return tokens;
},
'tokenNames': getNameTokens(),

Am I doing somthing wrong here?

5
  • What is inside world array and is it a global one? Is your code always returning one of the options and never the other one? Commented Dec 3, 2015 at 12:53
  • Can you explain what's wrong exactly? Are you getting an error or unexpected behaviour? Commented Dec 3, 2015 at 12:53
  • the code is causing the page to not load. I'm a hobbyist rather than a pro so I'm doing this all in the browser. Commented Dec 3, 2015 at 13:01
  • You must get an error message in the developer console. That message is the key. Is it like getNameTokens is not defined or something? Commented Dec 3, 2015 at 13:08
  • the world array works in other functions and variables in the same invironment that this function is created and called in. Commented Dec 3, 2015 at 13:08

3 Answers 3

1

Probability it should be something like this:

var world = [0,2,3,5,5,];

var getNameTokens = function(element) {
    var tokens = {};
    if(world.indexOf(element) <= 1) {
        tokens = {
          agent: 'president', 
          ent: 'company', 
          ownsf: 'the company\'s', 
          owns: 'its'
        };

    } else {
        tokens = {
          agent: 'player', 
          ent: 'player', 
          ownsf: 'his', 
          owns: 'his'
        };
    }
  return tokens;
}

var newObj = getNameTokens(0);
console.log(newObj)

Fiddle example. Or It could be also as @Krystian Laskowski has described

Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately, you cannot invoke object method in this object literal. That is because an object literal is only a list of key/value pairs. It doesn't contain any instantiation logic (i.e. constructor).

I guess that the solution for your problem might be something like this:

function objectFactory(){
var getNameTokens = function() {
    var tokens;
    if(world.indexOf('9') == 1 ||world.indexOf('9') == 0) {
        tokens = {agent: 'president', ent: 'company', ownsf: 'the company\'s', owns: 'its'};
    } else {
        tokens = {agent: 'player', ent: 'player', ownsf: 'his', owns: 'his'};
    }
    return tokens;
}
var result = {
    getNameTokens: getNameTokens,
    tokenNames: getNameTokens
}
return result;
}

Comments

0

It looks like your code is part of an object literal, like:

var world = '9876';
var Service = {
    getNameTokens: function() {
        var tokens;
        if(world.indexOf('9') == 1 ||world.indexOf('9') == 0) {
            tokens = {agent: 'president', ent: 'company', ownsf: 'the company\'s', owns: 'its'};
        } else {
            tokens = {agent: 'player', ent: 'player', ownsf: 'his', owns: 'his'};
        }
        return tokens;
    },
    'tokenNames': getNameTokens()
};

In this case, you can't just write 'tokenNames': getNameTokens(),, as there is no getNameTokens() function in the scope. There will be a Service.getNameTokens() function, but only after the object gets constructed. To use it during the construction define it separately:

var world = '9876';
function getNameTokens() {
    var tokens;
    if(world.indexOf('9') == 1 ||world.indexOf('9') == 0) {
        tokens = {agent: 'president', ent: 'company', ownsf: 'the company\'s', owns: 'its'};
    } else {
        tokens = {agent: 'player', ent: 'player', ownsf: 'his', owns: 'his'};
    }
    return tokens;
}
var Service = {
    getNameTokens: getNameTokens,
    'tokenNames': getNameTokens()
};

Comments

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.